在同一项目中将 EF DbContext 与 mysql 和 sql 结合使用

Using EF DbContext with mysql and sql in the same project

我正在开发一个 ASP.net MVC 项目,结合使用 EF 和 MySql。现在这本身就可以正常工作,但我还想引用另一个 class 库,该库将 EF 与 SQL.

结合使用

当我引用库时,EF 似乎对为每个 DbContext 使用哪个提供程序感到困惑。

在我的 MySql DbContext 上,我有以下属性来告诉 EF 这个 DbContext 应该由 MySql 提供商处理:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

现在在 SQL DbContext 上,我没有属性。我应该放一个在那里吗?如果是的话,放一个吗?我对此事进行了一些搜索,但在任何地方都找不到正确的答案。

目前,我收到以下错误:

The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered. An instance of 'MySqlEFConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file.

这非常简单,因为 SQL 上下文在 MySql 上下文之前使用,但我似乎找不到解决方法。

所以问题是 'Best Practice' 处理这个问题是什么?或者这是我应该避免的事情,在同一个项目中组合 2 个 DbContext。

在此先致谢!

MySql DbContext

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MySqlContext : DbContext
{
    public MySqlContext() : base("name=MySqlContext")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
    //DbSets....
}

SQL DbContext

public class SqlContext : DbContext
{
    public SqlContext() : base("name=SqlContext")
    {
        Configuration.LazyLoadingEnabled = false;
    }
    //DbSets....
}

Web.config:

    <connectionStrings>
    <add name="SqlContext" connectionString="some connectionString" providerName="System.Data.SqlClient" />
    <add name="MysqlContext" connectionString="some connectionString" 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, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

我在 DbProviderFactories 上看到它唯一的说法 Mysql。

您拥有多少 DbContext 并不重要(在 entity framework 6 中)。只需将连接字符串放在启动项目的 appConfig 或 webConfig 中即可。

带有两个 connectionString 的 appConfig 示例,Ef 6.01 & Sql Server 和 My SQL

<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="MySqlDB" connectionString="Your My SQL ConnectionString"  providerName="My Sql Provider" />
    <add name="SqlDB" connectionString="Your SQL ConnectionString"  providerName="Sql Provider" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="System.Data.SqlServerCe.4.0" />
  </parameters>
</defaultConnectionFactory>
<providers>
   <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>

MySql DbContext

public class MySqlContext : DbContext
{
   public MySqlContext() : base("MySqlDB")
   {
      this.Configuration.LazyLoadingEnabled = false;
   }
//DbSets....
}

SQL DbContext

public class SqlContext : DbContext
{
   public SqlContext() : base("SqlDB")
   {
      Configuration.LazyLoadingEnabled = false;
   }
   //DbSets....
}