在实体框架中使用MySQL和MSSQL 6 [英] Use MySQL and MSSQL in Entity Framework 6

查看:278
本文介绍了在实体框架中使用MySQL和MSSQL 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发webservice。



这个服务的背后有两种方法:
一个从MySQL Connection获取实体,另一个是获取实体从MSSQL服务器连接。



我有两个连接字符串。



我想要有两个上下文,他们是完全分开的。



但我无法管理这个。



任何想法?



确保安装所有目标系统上的 MySql Connector / Net
我错过了我的目标平台。



web.config / app.config:

 <结构> 
< configSections>
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< connectionStrings>
< add name =MsSqlServerContextconnectionString =MSSQLCONNECTIONSTRINGproviderName =System.Data.SqlClient/>
< add name =MySqlServerContextconnectionString =MYSQLCONNECTIONSTRINGproviderName =MySql.Data.MySqlClient/>
< / connectionStrings>
< entityFramework>
< defaultConnectionFactory type =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework>
< parameters>
< parameter value =mssqllocaldb/>
< / parameters>
< / defaultConnectionFactory>
< providers>
< provider invariantName =System.Data.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< provider invariantName =MySql.Data.MySqlClienttype =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>(空);
}

//在这里添加DbSets
public DbSet< ClassName1> SomeName1 {get;组; }
public DbSet< ClassName2> SomeName2 {get;组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//在此添加映射
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;组; }
public DbSet< ClassName4> SomeName4 {get;组;

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


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?

解决方案

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());
    }
}

这篇关于在实体框架中使用MySQL和MSSQL 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆