Is it possible to have an EF context for MySql and SqlServer?

I have two entity structure contexts, one for MySql and one for sql. If I run the application, I get the following error:

The default DbConfiguration instance was used by the Entity Framework before the     'MySqlEFConfiguration' type was discovered. 

However, if I found the database type when the application started, indicating to it Database.SetInitializer<MySqlContext>, my normal sql context now tries to connect using the mysql provider.

My mysql context

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ZipCodeContext : DbContext
{
    public DbSet<ZipCode> ZipCodes { get; set; }        
}

My normal context

public class MyContext : DbContext
{
    public DbSet<MyClass> MyClasses{get;set;}
}

and finally, my web configuration for the entity structure section

 <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

I hope this is clear enough, I knock my head on the table

+1
source share
2 answers

My Uow.cs 2 . DbConfiguration , . , .

. , Oracle MS SQL. db, DbConfigurationType. :

public class CustomDbConfiguration : DbConfiguration
{
    public CustomDbConfiguration()
    {
        if (DbChecker.MySqlInUse)
            SetConfiguration(new MySqlEFConfiguration());
    }
}

[DbConfigurationType(typeof(CustomDbConfiguration))]
public class ZipCodeContext : DbContext
{
    public DbSet<ZipCode> ZipCodes { get; set; }
}

[DbConfigurationType(typeof(CustomDbConfiguration))]
public class MyContext : DbContext
{
    public DbSet<MyClass> MyClasses { get; set; }
}
+1

FYIW - . , , .

var needsToBeInstantiated = new EFDBContextConfiguration();
EFDBContextConfiguration.SetConfiguration( needsToBeInstantiated );
0

Source: https://habr.com/ru/post/1539546/


All Articles