在同一项目中将EF DbContext与mysql和sql一起使用 [英] Using EF DbContext with mysql and sql in the same project

查看:105
本文介绍了在同一项目中将EF DbContext与mysql和sql一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

在MySql DbContext上,我具有以下属性,以便告诉EF该DbContext应该由MySql提供程序处理:

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))]

现在在SQL DbContext上,我没有属性.我应该在上面放一个吗?如果可以的话,放一个吗?我对此事做了一些搜索,但在任何地方都找不到正确的答案.

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:

实体框架使用默认的DbConfiguration实例在发现"MySqlEFConfiguration"类型之前.的实例使用之前必须在应用程序启动时设置"MySqlEFConfiguration"任何实体框架功能或必须在应用程序的配置文件.

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.

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

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.

那么问题是处理此问题的最佳实践"是什么?还是在同一个项目中合并两个DbContext,这是我应该避免的事情.

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.

感谢进阶!

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.

I see on the DbProviderFactories its only saying Mysql.

推荐答案

拥有多少DbContext(在实体框架6中)并不重要.只需将连接字符串放在启动项目的appConfig或webConfig中即可.

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.

具有两个具有Ef 6.01& amp;的connectionString的appConfig的示例Sql Server和My SQL

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

MySql DbContext

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

SQL DbContext

SQL DbContext

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

这篇关于在同一项目中将EF DbContext与mysql和sql一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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