Use MySQL and MSSQL in Entity Framework 6

≯℡__Kan透↙ 提交于 2019-12-11 09:44:17

问题


I'm developing a webservice.

Behind this service are two methods: One to get entities from a MySQL Connection and the other to get entities from a MSSQL Server Connection.

I have two connection strings.

I would like to have two contexts, they are completely separated.

But i'm not able to manage this.

Any ideas?


回答1:


The solution was simple in the end.

Be sure to install MySql Connector/Net on all target systems! I missed this on my target platform.

web.config / app.config:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MsSqlServerContext" connectionString="MSSQLCONNECTIONSTRING" providerName="System.Data.SqlClient" />
    <add name="MySqlServerContext" connectionString="MYSQLCONNECTIONSTRING" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
</configuration>

MsSqlServerContext.cs

public partial class MsSqlServerContext : DbContext
{
    public MsSqlServerContext()
        : base("name=MsSqlServerContext")
    {
        Database.SetInitializer<MsSqlServerContext>(null);
    }

    // Add DbSets here
    public DbSet<ClassName1> SomeName1 { get; set; }
    public DbSet<ClassName2> SomeName2 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Add Mappings here
        modelBuilder.Configurations.Add(new ClassName1Map());
        modelBuilder.Configurations.Add(new ClassName2Map());
    }
}

MySqlServerContext

public partial class MySqlServerContext : DbContext
{
    public MySqlServerContext()
        : base("name=MySqlServerContext")
    {
        Database.SetInitializer<MySqlServerContext>(null);
    }

    public DbSet<ClassName3> SomeName3 { get; set; }
    public DbSet<ClassName4> SomeName4 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ClassName3Map());
        modelBuilder.Configurations.Add(new ClassName4Map());
    }
}


来源:https://stackoverflow.com/questions/31280119/use-mysql-and-mssql-in-entity-framework-6

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