如何创建实体框架ObjectContext?

发布于 2024-12-02 12:14:34 字数 1336 浏览 0 评论 0原文

我在一台 SQL 服务器上有很多数据库。 我将 connectionString 作为模板(查看 Initial Catalog={0})放入 web.config 中。

我想使用正确的连接字符串创建 objectContext。我想执行以下操作 CreatObjectContext('MySite') 但收到错误无法确定类型为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼中杀气 2024-12-09 12:14:34

如果您使用EntityConnectionStringBuilder,则只需将sqlserver 连接字符串存储在web.config 中。然后,EntityConnectionStringBuilder 可以将它们转换为 EF4 连接字符串。

示例 web.config

    <connectionStrings>
      <add name="db1" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db1;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" /> 
      <add name="db2" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" />
    </connectionStrings>

我们可以将您的方法更改为:

public ObjectContext CreatObjectContext(string dbName)
{          
       var conStr = ConfigurationManager.ConnectionStrings[dbName].ConnectionString;
       var entityBuilder = new EntityConnectionStringBuilder();

       entityBuilder.Provider = "System.Data.SqlClient";
       entityBuilder.ProviderConnectionString = conStr;

       entityBuilder.MetaData = @"res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl";

       return new ObjectContext(entityBuilder.ToString());          
}

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

    <connectionStrings>
      <add name="db1" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db1;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" /> 
      <add name="db2" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" />
    </connectionStrings>

And we can change your method to something like:

public ObjectContext CreatObjectContext(string dbName)
{          
       var conStr = ConfigurationManager.ConnectionStrings[dbName].ConnectionString;
       var entityBuilder = new EntityConnectionStringBuilder();

       entityBuilder.Provider = "System.Data.SqlClient";
       entityBuilder.ProviderConnectionString = conStr;

       entityBuilder.MetaData = @"res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl";

       return new ObjectContext(entityBuilder.ToString());          
}
阳光①夏 2024-12-09 12:14:34

我只是想共享一个小类来使用实体类作为 T 类型的 SQL 连接字符串和 entityModel 元数据名称来创建实体框架连接。

    public static class EFConnection<T> where T : ObjectContext
    {
    public static T GetDatabase(string connectionString,string entityModelMetadataName)
    {

        var entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = connectionString;
        entityBuilder.Metadata = @"res://*/" + entityModelMetadataName + ".csdl|res://*/" + entityModelMetadataName + ".ssdl|res://*/" + entityModelMetadataName + ".msl";


    var _db=(T)Activator.CreateInstance(typeof(T), new object[] { entityBuilder.ToString()});
    return _db;

    }

}

使用示例:

    var _db = EFConnection<Model1Container>.GetDatabase(Settings.General.Default.DatabaseConnectionString, "Model1");

我确实使用这篇帖子也将所有内容放在一起:

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.

    public static class EFConnection<T> where T : ObjectContext
    {
    public static T GetDatabase(string connectionString,string entityModelMetadataName)
    {

        var entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = connectionString;
        entityBuilder.Metadata = @"res://*/" + entityModelMetadataName + ".csdl|res://*/" + entityModelMetadataName + ".ssdl|res://*/" + entityModelMetadataName + ".msl";


    var _db=(T)Activator.CreateInstance(typeof(T), new object[] { entityBuilder.ToString()});
    return _db;

    }

}

use example:

    var _db = EFConnection<Model1Container>.GetDatabase(Settings.General.Default.DatabaseConnectionString, "Model1");

I did use this post also to put everything together:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文