1

I have two entity framework contexts, one for MySql and one for sql. If I run the app I get the following error

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

However if I do discover the database type on app start by giving it the Database.SetInitializer<MySqlContext>, my normal sql context is now trying 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 config for the entity framework 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'm banging my head on the desk

tympaniplayer
  • 171
  • 2
  • 19

2 Answers2

1

My Uow.cs had 2 different contexts as variables. As a result during object initialization the first context's DbConfiguration was silently applied even when I was interested only in the second context. As a result the second context have responded with the same error until I have changed the design.

update

More details - I have updated the code to use the same Context for Oracle and MS SQL versions. To provide correspondent db configuration I have added custom config class in the same assembly with the context and use it with DbConfigurationType attribute. In your case it could be something like this:

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

and both contexts are trying to apply it

[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; }
}
0

FYIW - I had the same problem. The only thing that resolved the issue was to add the following code at the beginning of my program's execution.

var needsToBeInstantiated = new EFDBContextConfiguration();
EFDBContextConfiguration.SetConfiguration( needsToBeInstantiated );
CodeSlinger512
  • 618
  • 1
  • 8
  • 23