Mam wiele baz danych w jednym serwerze SQL. Umieściłem connectionString jako szablon (spójrz na Initial Catalog={0}
) na web.config.Jak utworzyć Entity Framework ObjectContext?
<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" />
Chcę utworzyć objectContext z prawidłowym connectionString. Pomyślałem, aby wykonać następujące czynności, CreatObjectContext<SiteEntities>('MySite')
, ale pojawia się błąd 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);
}
Co robię źle? Jak mogę stworzyć kontekst?
Powiązane pytanie entity-framework-runtime-connection string http://stackoverflow.com/ pytania/11368897/entity-framework-runtime-connection-string –