Hi RPA,
When you are calling the DbContext, you're calling the empty constructor (new NorthwindEntities()). Thus, it will call the auto-generated DbContext. If you want to call your partial class, you need to call it explicitly with the parameter.
For this you need to add another constructor with parameter.
Refering the above article i have created the example.
The Form consists of two DataGridView control one for displaying record from default generated Entity Framework ConnectionString and another from dynamic generated ConnectionString.
NorthwindModel.Context.cs
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class NorthwindEntities : DbContext
{
public NorthwindEntities()
: base("name=NorthwindEntities")
{
}
public NorthwindEntities(string connectionString)
: base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Employee> Employees { get; set; }
}
App.Config
<connectionStrings>
<add name="NorthwindEntities" connectionString="metadata=res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Northwind;user id=sa;password=pass@123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="constr" connectionString="Server=.;DataBase=master;UID=sa;PWD=pass@123" providerName="System.Data.SqlClient" />
</connectionStrings>
Namespaces
C#
using System.Configuration;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Linq;
using System.Windows.Forms;
VB.Net
Imports System.Configuration
Imports System.Data.EntityClient
Imports System.Data.SqlClient
Imports System.Linq
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
// From default generated Entity Framework ConnectionString.
NorthwindEntities entities1 = new NorthwindEntities();
dataGridView1.DataSource = (from p in entities1.Customers.Take(5)
select new
{
CustomerId = p.CustomerID,
ContactName = p.ContactName,
Country = p.Country
}).ToList();
// From dynamic generated ConnectionString.
string connectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(connectionString);
EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
ecb.ProviderConnectionString = scsb.ConnectionString;
ecb.Provider = "System.Data.SqlClient";
ecb.Metadata = "res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl";
NorthwindEntities entities = new NorthwindEntities(ecb.ConnectionString);
dataGridView2.DataSource = (from p in entities.Employees.Take(5)
select new
{
EmployeeId = p.EmployeeID,
Name = p.FirstName + " " + p.LastName,
Country = p.Country
}).ToList();
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' From default generated Entity Framework ConnectionString.
Dim entities1 As NorthwindEntities = New NorthwindEntities()
dataGridView1.DataSource = (From p In entities1.Customers.Take(5)
Select New With {
.CustomerId = p.CustomerID,
.ContactName = p.ContactName,
.Country = p.Country
}).ToList()
' From dynamic generated ConnectionString.
Dim connectionString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim scsb As SqlConnectionStringBuilder = New SqlConnectionStringBuilder(connectionString)
Dim ecb As EntityConnectionStringBuilder = New EntityConnectionStringBuilder()
ecb.ProviderConnectionString = scsb.ConnectionString
ecb.Provider = "System.Data.SqlClient"
ecb.Metadata = "res://*/NorthwindModel.csdl|res://*/NorthwindModel.ssdl|res://*/NorthwindModel.msl"
Dim entities As NorthwindEntities = New NorthwindEntities(ecb.ConnectionString)
dataGridView2.DataSource = (From p In entities.Employees.Take(5)
Select New With {
.EmployeeId = p.EmployeeID,
.Name = p.FirstName & " " & p.LastName,
.Country = p.Country
}).ToList()
End Sub
Screenshot