locked
DbConfiguration - problem to hold twice RRS feed

  • Question

  • 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

    Monday, June 5, 2017 8:31 AM

All replies

  • 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


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 6, 2017 5:59 AM