我在一台 SQL 服务器中有许多数据库。我将 connectionString 作为模板(查看Initial Catalog={0}
)放入 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" />
我想用正确的 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);
}
我做错了什么?我如何创建上下文?