我在一個SQL服務器中有很多數據庫。 我將connectionString作爲模板(查看Initial Catalog={0}
)放入web.config中。如何創建實體框架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" />
我想用正確的connectionString創建ObjectContext的。我想要做到以下幾點,CreatObjectContext<SiteEntities>('MySite')
但我得到錯誤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);
}
我做錯了什麼?我如何創建上下文?
相關問題實體框架運行時連接字符串http://stackoverflow.com/問題/ 11368897 /實體框架運行時連接字符串 –