Two different providers on the same config file

不羁岁月 提交于 2019-12-03 08:24:15

First of all you are using the wrong configuration classes. The DbConfigurationType needs a type inherited from DbConfiguration not DbMigrationsConfiguration<>.

The DbMigrationsConfiguration is really just used for the Migrators and DatabaseInitializers.

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        this.SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"));

        this.SetProviderServices("System.Data.SqlServerCe.4.0", SqlCeProviderServices.Instance);

        this.AddInterceptor(new NLogCommandInterceptor());// guardar logs

        this.SetMigrationSqlGenerator("System.Data.SqlServerCe.4.0", () => new SqlCeMigrationSqlGenerator());
    }
}

[DbConfigurationType(typeof(MyDbConfiguration))]
public class TestContext : DbContext

Sadly it is not possible so set multiple DefaultConnectionFactories even with multiple DbConfigurations.

In your case you will have to store the connection strings in the app.config and pass the name to the DbContext constructor.

public class TestContext : DbContext
    {
        public TestContext()
            : base("name=MyConnectionString")
        {

        }

The connection will be initialized based on the provider name for MyConnectionString in the app.config

Or if you don’t want the connection string in your app.config just pass an already initialized DbConnection to the DbContext constructor

public class TestContext : DbContext
    {
        public TestContext()
            : base(new SqlCeConnection(GetConnectionString()),true)
        {

        }

Or if you don’t want to initialize a specific Connection use the DbProviderFactory.

public class TestContext : DbContext
    {
        public TestContext()
            : base(GetConnection(),true)
        {

        }

        public static DbConnection GetConnection() { 
            var factory = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0");
            var connection = factory.CreateConnection();
            connection.ConnectionString = "Data Source=C:/teste2.sdf;Persist Security Info=False;";
            return connection;
        }

RESOLVED! (for my case)

My Config file

  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"></remove>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0" />
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
</configuration>

The 2 contexts are the same as the question. But i removed the [DbConfigurationType(typeof(Sistema.DataAccess.Migrations.Configuration))] from the 2 contexts

Config class 1 (mysql)

public sealed class Configuration : DbMigrationsConfiguration<Sistema.DataAccess.SistemaContext>
    {
        public Configuration()
        {
            DbConfiguration.SetConfiguration(new DbConfigurationBase("MYSQL"));

            DbInterception.Add(new NLogCommandInterceptor());// guardar logs

            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true; 

            SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());//Mysql da erro se nao colocar isso.(Pelo que vi da para colocar no App.config tambem.)
            SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) => new MySQLHistoryContext(conn, schema));
        }
}

Config Class 2 (SQLCE)

public sealed class Configuration2 : DbMigrationsConfiguration<Sistema.DataAccess.SistemaContext2>
    {
        public Configuration2()
        {
            DbConfiguration.SetConfiguration(new DbConfigurationBase("SQLCE"));

            DbInterception.Add(new NLogCommandInterceptor());// guardar logs

            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
}

And i used your DBConnection Idea to create the connection.

You need to specify the respective provider as part of the connection string. I usually do this all in config, so something like this:

<connectionStrings>
  <add name="connString1" connectionString="..." providerName="MySql.Data.MySqlClient"<entityFramework>
  <add name="connString2" connectionString="..." providerName="System.Data.SqlServerCe.4.0"<entityFramework>
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
        <parameters>
          <parameter value="System.Data.SqlServerCe.4.0" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
        <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
     </providers>
</entityFramework>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!