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

橙三吉。 提交于 2019-11-30 06:56:29

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.

GaussZ

You can use the context constructor that takes a DBConnection and provide the correct connection to the context.

For an example see this question: EF6 and multiple configurations (SQL Server and SQL Server Compact)

Jay Carlton

I had the same error, and in my case I had a DbConfiguration class defined in another assembly. Even though it was in the same dll as the DbContext, I think it only catches this at runtime because of lazy loading or reflection or something.

The best way I've found was to set it in web.config or app.config using the codeConfigurationType attribute on the entityFramework node as described here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!