DbConfiguration - 持有两次的问题 [英] DbConfiguration - problem to hold twice

查看:108
本文介绍了DbConfiguration - 持有两次的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个项目类型DLL,每个都有实体FrameWork 6 - 使用DbConfiguration


类似的东西

 

公共课 MyConfiguration DbConfiguration
{
public MyConfiguration()
{

}
}

 [ DbConfigurationType(typeof( MyDbConfiguration ))] 
公共类MyContextContext:DbContext
{
}

运行时的每个DLL只有自己(来自另一个exe项目) - 工作正常。

但是 - 当我打开新项目(WPF)并引用两个DLL时 - 代码失败。

我已经在网上看到了这个事实

"注意:   EF不支持在同一个AppDomain中使用多个配置类"

请帮我解释一下如何解决这个问题。

谢谢

Yehuda

解决方案

Hi Entity FrameWork6,


请使用名称空间和程序集名称的特殊配置类,如下所示:

 [DbConfigurationType ("MyNamespace.MyConfiguration,MyAssemblyFullyQualifiedName")] 
公共类MyContextContext:DbContext
{
}

如果不起作用。请检查以下解决方法,将Context与构造函数DbConnection一起使用。

 public class MyContextContext:DbContext {
//在MyMigrationsContextFactory $ b中管理MIgration无参数构造函数
$ b public MyContextContext(string connectionName):base(connectionName){} // no this

public MyContextContext(DbConnection dbConnection,bool contextOwnsConnection)// THIS ONE
:base (dbConnection,contextOwnsConnection){}
}


然后你可以使用"DBConnection"每个提供商的连接。对于SQL服务器

 public DbConnection GetSqlConn4DbName(string dataSource,string dbName){
var sqlConnStringBuilder = new SqlConnectionStringBuilder ();
sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource)? DefaultDataSource:dataSource;
sqlConnStringBuilder.IntegratedSecurity = true;
sqlConnStringBuilder.MultipleActiveResultSets = true;

var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
var sqlConn = sqlConnFact.CreateConnection(dbName);
返回sqlConn;
}

祝你好运,


Cole Wu


Hi,

I have two project type DLL , each one has Entity FrameWork 6 - using DbConfiguration

something like this

public class MyConfiguration : DbConfiguration { public MyConfiguration() { } }

[DbConfigurationType(typeof(MyDbConfiguration))] 
public class MyContextContext : DbContext 
{ 
}

Each DLL when i run it only by himself (from another exe project) - works just fine.

BUT - when i open new project (WPF) and have reference to both DLL - the code fail.

I have read in the the internet this fact

"Note: EF does not support having multiple configuration classes used in the same AppDomain"

Please help me by give an idea how to solve this problem.

Thank you

Yehuda

解决方案

Hi Entity FrameWork6,

Please special configuration class with namespace and assembly name, like this:

[DbConfigurationType("MyNamespace.MyConfiguration, MyAssemblyFullyQualifiedName")]
public class MyContextContext : DbContext
{
}

if it does not works. please check the following workaround, Use the Context with constructor DbConnection .

public class MyContextContext : DbContext {
     // MIgration parameterless constructor is managed in  MyMigrationsContextFactory 

    public MyContextContext(string connectionName) : base(connectionName) { } // no this

    public MyContextContext(DbConnection dbConnection, bool contextOwnsConnection)  // THIS ONE
        : base(dbConnection, contextOwnsConnection) {  }
}

Then you could use a "DBConnection" connection for each provider. For SQL server

public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
        var sqlConnStringBuilder = new SqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;

        var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    }

Best regards,

Cole Wu


这篇关于DbConfiguration - 持有两次的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆