c# - 在同一项目(SQL Server 和 MySql)中使用具有多个数据库和提供程序的 Entity Framework

标签 c# .net entity-framework entity-framework-6 mysql-connector

背景:

我有一个应用程序(Web 表单和控制台),它首先使用实体​​框架代码轻松访问多个 SQL Server 数据库,没有任何问题。我可以在上下文之间来回切换而不会出错。现在我正在尝试添加一个新的 MySql 数据库(不同的提供者),但是如果我从一个上下文切换到另一个上下文,我会收到以下错误消息:

错误:

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. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.



我见过建议将“codeConfigurationType”属性添加到 web.config 中的 entityFramework 节点以指定我的 MySql 程序集的解决方案,但这将不起作用,因为我的目标是具有多个提供程序的多个数据库。

Microsoft 支持文章似乎暗示创建我自己的自定义 DbConfiguration 文件,但我已经有了一个:MySql.Data.Entity.mySqlEFConfiguration,并且我已经用它相应地装饰了我的 DbContext:

   [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class MySqlDBContext : DbContext
    {
        public MySqlDBContext() : base("MySqlDBContext")
        {

        }
    }
...

这是我如何设置 web.config:

  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>        
    <add name="FirstSQLServerDBContext" providerName="System.Data.SqlClient" connectionString="Server=SQLServerName;MultipleActiveResultSets=True; Database=FirstSQLDB; User ID=SQLUsername;Password=SqlPassword; Encrypt=true;" />
    <add name="SecondSQLServerDBContext" providerName="System.Data.SqlClient" connectionString="Server=SQLServerName;MultipleActiveResultSets=True; Database=SecondSQLDB; User ID=SQLUsername;Password=SqlPassword; Encrypt=true;" />
    <add name="MySqlDBContext" providerName="MySql.Data.MySqlClient" connectionString="server=MySqlServerName; port=3306;database=MySqlDB; uid=MySqlUsername; password=MySqlPassword;" />    
  </connectionStrings>

题:

似乎没有任何方法可以使用配置文件配置来做到这一点,因为我仅限于一个条目。那么如何在切换上下文时指定提供者呢?我想在每个 DbContext 的构造函数中我都缺少一些东西来明确说明要使用哪个提供程序?作为记录,我在控制台应用程序和 Web 表单应用程序中都收到了相同的错误消息。

更新:附加信息
  • (8/21/19) 如果我在 SQLServer 数据源上禁用 MultipleActiveResultSets (MARS) 似乎可以正常工作,尽管这不是理想的解决方案,因为我已经编写了代码来利用 MARS。
  • (8/22/19) 其实那不会解决它。它会继续解决下一个问题。每个应用程序只能有一个 DbConfiguration,因此该解决方案可能需要编写一个适用于两个提供程序的程序...我已经通过我的 MSDN 订阅提交了一份支持请求,以查看他们是否有任何其他见解...
  • 最佳答案

    在微软的帮助下,我终于弄明白了。希望这可以帮助其他人。我必须创建自己的类继承自 DbConfiguration

    public class MyCustomSQLDbConfiguration : DbConfiguration
    {
        public MyCustomSQLDbConfiguration()
        {
            SetExecutionStrategy("MySql.Data.MySqlClient", () => new MySqlExecutionStrategy());
            SetDefaultConnectionFactory(new LocalDbConnectionFactory("mssqllocaldb"));
        }
    }
    

    然后相应地装饰我的 MySqlDBContext:
    [DbConfigurationType(typeof(MyCustomSQLDbConfiguration))]
    public class MySqlDBContext : DbContext
    {
        public MySqlDBContext() : base("MySqlDBContext")
        {
    
        }
     }
    

    最后(也是重要的),当我运行应用程序时明确设置它。对于控制台应用程序,在 Program.Main 的开头,或在 Web 应用程序中,在 Application_Start 上:
    DbConfiguration.SetConfiguration(new MyCustomSQLDbConfiguration());
    

    关于c# - 在同一项目(SQL Server 和 MySql)中使用具有多个数据库和提供程序的 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57595183/

    相关文章:

    c# - 如何在 IWebDriver 中使用 IE 选项?

    entity-framework - EF 4.1,POCO : Is any way to get Table name in runtime to avoid hardcode?

    c# - ASP.NET MVC 5 Entity Framework - 关系

    c# - 从 ViewModel 获取 [key] 属性

    c# - 为什么我不能在接口(interface)上调用 ToString()?

    c# - 如何在没有加载项的情况下从 .NET 启动 MS Office Word?

    .net 收藏 : how to copy objects from one collection to another?

    c# - 寻找如何重构我的算法的想法

    c# - 命名空间被编译器混淆

    c# - EntityFramework - 在 LINQ 查询中按无序对分组