Using EF DbContext with mysql and sql in the same project

我们两清 提交于 2021-02-16 15:23:47

问题


I'm working on an ASP.net MVC project using EF in combination with MySql. Now this works just fine on its own but I also want to reference another class library that uses EF with SQL.

When I reference the library EF seems to get confused on what provider to use for each DbContext.

On my MySql DbContext I have the following attribute in order to tell EF this DbContext should be handled by the MySql provider:

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

Now on the SQL DbContext, I have no attribute. Should I place one on there and if so wich one? I did some search on this matter but could not find the right answer anywhere.

Currently, I get the following error:

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.

This is pretty straight forward since the SQL context is used before the MySql one but I can't seem to find a fix on this.

So the question is what would be 'Best Practice' on handling this? Or is this something that I should avoid, combining 2 DbContexts in the same project.

Thanks in advanced!

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>

I see on the DbProviderFactories its only saying Mysql.


回答1:


It's not important that how many DbContexts you have(In entity framework 6). Just put connection strings in appConfig or webConfig of startup project.

Example of appConfig with two connectionString with Ef 6.01 & Sql Server and 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....
}


来源:https://stackoverflow.com/questions/38452314/using-ef-dbcontext-with-mysql-and-sql-in-the-same-project

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