gpt4 book ai didi

mysql - 使用 Entity Framework 将 MySQL 和 MSSQL 用于两个不同的数据库

转载 作者:可可西里 更新时间:2023-11-01 07:06:14 25 4
gpt4 key购买 nike

我正在尝试构建一个 Web API 来提供来自 MySQL 数据库或 MSSQL 数据库(完全不同的数据和模式)的数据。它还提供了一些端点,用于在两者之间移动数据。我已经创建了两个类库来保存两个数据库的 EF 模型,并且已经成功地连接到两个实例并对两个实例运行查询。它失败的地方是试图访问它们。我必须调整 web.config entityFramework 部分才能使其工作,但我无法让它同时有效地工作。我正在使用 EF6 和最新的 MySQL 连接器。

这是 MSSQL 实体的工作配置:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<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>

这是我尝试使用 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.

这是 MySQL 实体的工作配置:

<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" />

这会在尝试使用 MySQL 库时提示连接字符串。我在网上找不到太多关于这个的信息,这里的这个问题很相似,但没有答案:Is it possible to have a EF Context for MySql and SqlServer?

有没有人这样做过或知道解决这个问题的方法?

最佳答案

为了解决这个错误,我把它放在我的 App.config 中:"codeConfigurationType"

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">

为什么?可能配置没有找到 MySqlEFConfiguration 的位置。

我在这里只使用 Mysql我不知道这是否适用于 SQLServer 和 Mysql togueter。

对于您的问题,此链接可能有用:Link

你可以有 2 个分离的 configuration.cs 文件。一个用于 MySql,另一个用于 MSSQL

关于mysql - 使用 Entity Framework 将 MySQL 和 MSSQL 用于两个不同的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23514648/

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