gpt4 book ai didi

mysql - 相同的应用程序,不同的数据库 : Entity framework 6. X + MySQL + SQL Server

转载 作者:IT老高 更新时间:2023-10-29 00:21:26 25 4
gpt4 key购买 nike

昨天我做了 Web 应用程序的迁移 (EF 5.0 => EF 6.0),该应用程序使用 Entity Framework 访问 MySql 和 SQL Server 数据库(特别是 DbContext 到特定数据库,而不是任何 DbContext 到任何类型的数据库)。

编译时一切顺利,运行时出现异常:

The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered.

上下文中的 [DbConfigurationType(typeof(MySqlEFConfiguration))] 属性在运行时似乎已被忽略,因为上下文位于外部程序集 (?) 中并且 DbConfiguration 所使用的对应用程序域来说是全局的,而不是特定于上下文的(?)。”

我尝试了不同的方法来修复它,然后进行了谷歌搜索,但令人惊讶的是,我没有找到有效的解决方案。

看起来这里描述的情况很好http://forums.mysql.com/read.php?174,614148,614148仍然没有改变,或者我错过了一些明显的事情。

我们将不胜感激任何反馈。

提前谢谢你!

详细说明:

输入(简化):- ASP.NET 网络应用程序

  • 在 Entity Framework 6.1.1 上实现的数据访问层

  • Entity Framework 提供商:

    • System.Data.SqlClient 6.1.1

    • MySql.Data.MySqlClient 6.9.4

  • MY_SqlContext,模型优先概念,针对 MY SQL Server 数据库

  • Ms_SqlContext,数据库优先概念,针对MS SQL Server数据库

根据 Entity Framework 6 的通用文档和 MySql Connector/Net 文档( http://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html ),MY_SqlContext 需要应用 MySqlEFConfiguration。

根据上面提到的两个文档,有三个选项可以做到这一点。这三个都尝试过但都失败了。

选项 1:将 DbConfigurationTypeAttribute [DbConfigurationType(typeof(MySqlEFConfiguration))] 添加到 MY_SqlContext 类

适当的 Web.config 段:

<connectionStrings>
<add name="MY_SqlContext"
connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
providerName="MySql.Data.MySqlClient" />
<add name="Ms_SqlContext"
connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&quot;"
providerName="System.Data.EntityClient" />
</connectionStrings>

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, 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>

应用程序启动并开始处理 Web 请求后:

Ms_SqlContext 工作正常但尝试创建 MY_SqlContext 实例时,出现异常:

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 ...LinkId=260883 for more information."

选项 2:在应用程序启动时调用 DbConfiguration.SetConfiguration(new MySqlEFConfiguration())

适当的 Web.config 段(实际上与选项 1 相同):

<connectionStrings>
<add name="MY_SqlContext"
connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
providerName="MySql.Data.MySqlClient" />
<add name="Ms_SqlContext"
connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&quot;"
providerName="System.Data.EntityClient" />
</connectionStrings>

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, 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>

代码,添加到 Global.asax.cs: private void Application_Start(object sender, EventArgs e) { DbConfiguration.SetConfiguration(new MySqlEFConfiguration()); ...

在应用程序启动并且 Web 请求开始处理以尝试创建 Ms_SqlContext 实例后,我得到异常:

An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'Ms_SqlContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See ...?LinkId=260883 for more information.

方案三:在配置文件中设置DbConfiguration类型

适当的 Web.config 段

<connectionStrings>
<add name="MY_SqlContext"
connectionString="server=.;User Id=root;password=...;Persist Security Info=True;database=MySqlDb;Use Compression=False;Use Old Syntax=False"
providerName="MySql.Data.MySqlClient" />
<add name="Ms_SqlContext"
connectionString="metadata=res://*/Ms_SqlContext.csdl|res://*/Ms_SqlContext.ssdl|res://*/Ms_SqlContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MsSqlDb;User ID=appuser;Password=...&quot;"
providerName="System.Data.EntityClient" />
</connectionStrings>

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, 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>

应用程序启动并开始处理 Web 请求后: ...Ms_SqlContext 实例已创建,但在第一次执行查询期间,出现异常:

EntityException: {"The underlying provider failed on Open."}

InnerException: {"Unable to connect to any of the specified MySQL hosts."}

所以,Ms_SqlContext 得到的MySql 配置明显是错误的。

最佳答案

所以,最终的解决方案是:

  1. 使用 blackjack 和 hookers 创建自己的 DbConfiguration 继承者:

        public class MultipleDbConfiguration : DbConfiguration
    {
    #region Constructors

    public MultipleDbConfiguration()
    {
    SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
    }

    #endregion Constructors

    #region Public methods

    public static DbConnection GetMySqlConnection(string connectionString)
    {
    var connectionFactory = new MySqlConnectionFactory();

    return connectionFactory.CreateConnection(connectionString);
    }

    #endregion Public methods
    }
  2. 用 MultipleDbConfiguration 标记 Ms_SqlContext(不要对这种 DbContext 做任何其他事情)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]
    partial class Ms_SqlContext
    {
    }
  3. 使用 MultipleDbConfiguration 标记 Ms_SqlContext,并通过调用 MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString) 调整 MY_SqlContext(string nameOrConnectionString)

        [DbConfigurationType(typeof(MultipleDbConfiguration))]
    partial class MY_SqlContext : DbContext
    {
    public MY_SqlContext(string nameOrConnectionString) : base(MultipleDbConfiguration.GetMySqlConnection(nameOrConnectionString), true)
    {}
    }
  4. 就是这样!!!

关于mysql - 相同的应用程序,不同的数据库 : Entity framework 6. X + MySQL + SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26361592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com