We are in a mixed environment where our application is using both ADO.NET and Entity Framework.
Since both are pointing to the same physical SQL server, we would like to
You can work at design time using the connection string in your config file.
<add name="DWContext" connectionString="metadata=res://*/Database.DWH.DWModel.csdl|res://*/Database.DWH.DWModel.ssdl|res://*/Database.DWH.DWModel.msl;provider=System.Data.SqlClient;provider connection string="data source=SQLSERVER_INSTANCE;initial catalog=DB_NAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
So don't remove it because you need it ONLY at design time.
Work instead in a dynamic way at runtime using this approach (similar to your last one):
Extend the data context partial class:
public partial class DWContext
{
public DWContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
/// <summary>
/// Create a new EF6 dynamic data context using the specified provider connection string.
/// </summary>
/// <param name="providerConnectionString">Provider connection string to use. Usually a standart ADO.NET connection string.</param>
/// <returns></returns>
public static DWContext Create(string providerConnectionString)
{
var entityBuilder = new EntityConnectionStringBuilder();
// use your ADO.NET connection string
entityBuilder.ProviderConnectionString = providerConnectionString;
entityBuilder.Provider = "System.Data.SqlClient";
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/Database.DWH.DWModel.csdl|res://*/Database.DWH.DWModel.ssdl|res://*/Database.DWH.DWModel.msl";
return new DWContext(entityBuilder.ConnectionString);
}
}
And from your code create a new EF data context with:
private DWContext db = DWContext.Create(providerConnectionString);
Ciao ;-)
Other way is to declare another connection string in the config file and use it with the following alternative constructor:
class TestEntities : DbConnect {
public TestEntities (string connectionName)
: base($"name={connectionName}")
{
}
...
Then to use this solution just:
<add name="other" connectionString="metadata=res://*/Data.TestEntities .csdl|res://*/Data.TestEntities .ssdl|res://*/Data.TestEntities .msl;provider=System.Data.SqlClient;provider connection string="data source=...;initial catalog=...;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
call it like this:
var db = new TestEntities (connectionName:"other");
You are getting the Code First mode exception because you are passing a DbConnection
built with the ADO.NET connection string. This connection string does not include references to metadata files, so EntityFramework does not know where to find them.
To create a DbContext
with an appropriate programmatically set connection string, use the EntityConnectionStringBuilder class.
var entityBuilder = new EntityConnectionStringBuilder();
// use your ADO.NET connection string
entityBuilder.ProviderConnectionString = conString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl";
var dbContext = new DbContext(entityBuilder.ConnectionString);