Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Mesma aplicação, bancos de dados diferentes:Entity framework 6.X + MySQL + SQL Server


Então, a solução final é:

  1. Crie seu próprio sucessor DbConfiguration com blackjack e prostitutas:
        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. Marque Ms_SqlContext com MultipleDbConfiguration (e não faça mais nada com esse tipo de DbContext)
        [DbConfigurationType(typeof(MultipleDbConfiguration))]
        partial class Ms_SqlContext
        {
        }
    

  3. Marque Ms_SqlContext com MultipleDbConfiguration e ajuste MY_SqlContext(string nameOrConnectionString) com chamada MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString)
        [DbConfigurationType(typeof(MultipleDbConfiguration))]
        partial class MY_SqlContext : DbContext
        {
                    public MY_SqlContext(string nameOrConnectionString) : base(MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString), true)
                    {}
        }
    

  4. É ISSO!!!