I have many DBs in one SQL server.
I placed connectionString as template(look at Initial Catalog={0}
) into web.config.
<add name="ent" connectionString="metadata=res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl;provider=System.Data.SqlClient;provider connection string="Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
I want to create the objectContext with correct connectionString. I thought to do the following, CreatObjectContext<SiteEntities>('MySite')
but I get error Unable to determine the provider name for connection of type 'System.Data.EntityClient.EntityConnection'
.
public T CreatObjectContext<T>(string dbName) where T : ObjectContext, new()
{
var conStr = ConfigurationManager.ConnectionStrings["ent"].ConnectionString;
var entityBuilder = new EntityConnectionStringBuilder(conStr);
entityBuilder.Provider = "System.Data.SqlClient";
// Build correct conString to the db
entityBuilder.ProviderConnectionString = string.Format(entityBuilder.ProviderConnectionString, dbName);
var connection = new EntityConnection(entityBuilder.ConnectionString);
var builder = new ContextBuilder<T>();
return builder.Create(connection);
}
What I'm doing wrong? How I can create the context?