SQL >> Database >  >> RDS >> Mysql

Twee Entity Framework-providers in één configuratiebestand

Ten eerste gebruik je de verkeerde configuratieklassen. DbConfigurationType heeft een type nodig dat is afgeleid van DbConfiguration, niet DbMigrationsConfiguration<> — dat laatste is in feite alleen voor migrators en database-initializers.

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 

Helaas kun je niet meerdere default connection factories registreren, zelfs niet met meerdere DbConfiguration-klassen.

Leg in jouw geval de connection strings in app.config en geef de naam door aan de DbContext-constructor:

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

De provider wordt dan afgeleid uit het providerName-attribuut van MyConnectionString in app.config.

Wil je de connection string liever niet in app.config bewaren, geef de constructor dan een reeds geïnitialiseerde DbConnection:

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

Of sla een concrete verbinding helemaal over en ga via de 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; } } 
  1. De werkelijke kosten van dubbele indexes op dezelfde kolommen

  2. Prestaties van query's over twee MySQL-databases op dezelfde server?

  3. SQL — rijen selecteren met dezelfde waarde in twee kolommen

  4. Kan twee verschillende apparaten dezelfde GCM-registratie-id hebben?