如何创建实体框架ObjectContext?
我在一台 SQL 服务器上有很多数据库。 我将 connectionString 作为模板(查看 Initial Catalog={0}
)放入 web.config 中。
我想使用正确的连接字符串创建 objectContext。我想执行以下操作 CreatObjectContext
但收到错误无法确定类型为“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);
}
我做错了什么?我如何创建上下文?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用EntityConnectionStringBuilder,则只需将sqlserver 连接字符串存储在web.config 中。然后,EntityConnectionStringBuilder 可以将它们转换为 EF4 连接字符串。
示例 web.config
我们可以将您的方法更改为:
If you are using EntityConnectionStringBuilder, you only need to store the sqlserver connection strings in your web.config. EntityConnectionStringBuilder can then convert those to EF4 connection strings.
Example web.config
And we can change your method to something like:
我只是想共享一个小类来使用实体类作为 T 类型的 SQL 连接字符串和
entityModel
元数据名称来创建实体框架连接。使用示例:
我确实使用这篇帖子也将所有内容放在一起:
I just wanted to share a small class to create an entity framework connection using the entity class as type of T an SQL connection string and the
entityModel
meta data name.use example:
I did use this post also to put everything together: