Mysql
 sql >> Database >  >> RDS >> Mysql

Stessa applicazione, database diversi:Entity Framework 6.X + MySQL + SQL Server

Quindi, la soluzione finale è:

  1. Crea il tuo successore DbConfiguration con blackjack e prostitute:

        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. Contrassegna Ms_SqlContext con MultipleDbConfiguration (e non fare nient'altro con quel tipo di DbContext)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]
        partial class Ms_SqlContext
        {
        }
    
  3. Contrassegna Ms_SqlContext con MultipleDbConfiguration e ajust MY_SqlContext(string nameOrConnectionString) con la chiamata MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString)

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