使用MySQL和使用实体框架的两个不同数据库的MSSQL [英] Using MySQL & MSSQL for two different databases with Entity Framework

查看:145
本文介绍了使用MySQL和使用实体框架的两个不同数据库的MSSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a web api that serves data from either a MySQL database or a MSSQL database (completely different data and schema). It also provides some endpoints for moving data between the two. I have created two class libraries to hold the EF models for both databases and have been successful in connecting to both instances and running queries against both. Where it falls down is trying to access both of them. I have to tweak the web.config entityFramework section to get either to work, I can't get it so that both work at the same time effectively. I am using EF6 and the latest MySQL connector.

这是MSSQL实体的有效配置:

This is the working config for MSSQL entities:

<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实体上下文时产生的错误

This is the error it produces when I try to use the MySQL entity context

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实体的有效配置:

This is the working config for MySQL entities:

<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库.我在网上找不到很多,这里的问题与此类似,但没有答案:

This then complains about the connection string as its trying to use the MySQL libraries. I can't find much online about this, this question here is similar but there was no answer to it: Is it possible to have a EF Context for MySql and SqlServer?

有人这样做或知道解决此问题的方法吗?

Has anyone done this or know of a way round the issue?

推荐答案

在编写此版本6.9.1时,测试版MySQL连接器似乎已解决了该问题.我现在使两个并排运行而没有任何问题.

The issue appears to have been addressed in the beta MySQL connector at the time of writing this, version 6.9.1. I now have the two running side by side without any problems.

更新

为澄清起见,我使用的设置是EF5(EF6无法使用)和MySQL连接器版本6.9.1(测试版).我的web.config部分看起来像这样:

For clarification the setup I have is EF5 (EF6 won't work) and the MySQL connector version 6.9.1 (beta). my web.config section looks like this:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

模型位于同一项目中,同样,我在使用类库时遇到了困难.

The model's are located in the same project, again I have had difficulty with class libraries.

这篇关于使用MySQL和使用实体框架的两个不同数据库的MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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