Same application, different databases: Entity framework 6.X + MySQL + SQL Server Same application, different databases: Entity framework 6.X + MySQL + SQL Server mysql mysql

Same application, different databases: Entity framework 6.X + MySQL + SQL Server


So, final solution is:

  1. Create own DbConfiguration successor with blackjack and hookers:

        public class MultipleDbConfiguration : DbConfiguration    {        #region Constructors         public MultipleDbConfiguration()        {            SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());        }        #endregion Constructors        #region Public methods         public static DbConnection GetMySqlConnection(string connectionString)        {            var connectionFactory = new MySqlConnectionFactory();            return connectionFactory.CreateConnection(connectionString);        }        #endregion Public methods    }   
  2. Mark Ms_SqlContext with MultipleDbConfiguration (and do nothing else with that kind of DbContext)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]    partial class Ms_SqlContext    {    }
  3. Mark Ms_SqlContext with MultipleDbConfiguration, and ajust MY_SqlContext(string nameOrConnectionString) with call MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]    partial class MY_SqlContext : DbContext    {                public MY_SqlContext(string nameOrConnectionString) : base(MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString), true)                {}    }
  4. THAT IS IT!!!


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 );


You are correct that there is only one DbConfiguration per AppDomain rather than one per context. There is more information on how to specify it here - http://msdn.microsoft.com/en-us/data/jj680699#Moving. If you have multiple configurations and want to be sure the correct one is loaded then you probably want the config file option of the static DbConfiguration.SetConfiguration method.

It looks like MySQL is replacing a bunch of services in the dependency resolver, but the implementations they are registering only work for MySQL.

Code-based configuration is really designed for end developers to set up their config rather than for individual providers to ship a pre-baked one (since there is only one per AppDomain).

My recommendation is to not use theirs, create you own and only register services that you need/want to. For example their execution strategy would be a good thing to register - and registration is provider specific:

SetExecutionStrategy(MySqlProviderInvariantName.ProviderName, () => new MySqlExecutionStrategy());

There will probably be a bit of trail and error as you work out exactly what needs to be registered in order for their provider to work.