5

我在一台 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=&quot;Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True&quot;" 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);           
}

我做错了什么?我如何创建上下文?

4

2 回答 2

4

如果您使用的是 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());          
}
于 2011-08-31T21:15:37.823 回答
2

我只是想共享一个小类来创建实体框架连接,使用实体类作为 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");

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

于 2013-04-04T09:19:36.197 回答