我正在尝试构建一个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库。我在网上找不到很多,这里的问题是类似的,但是没有答案:是否可以为MySql和SqlServer使用EF上下文?
有没有人这样做或知道如何解决这个问题?
在编写此版本6.9.1时,似乎已在beta MySQL连接器中解决了该问题。我现在两个并排运行,没有任何问题。
更新
为了澄清起见,我使用的设置是EF5(EF6无法使用)和MySQL连接器版本6.9.1(测试版)。我的web.config部分如下所示:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
该模型位于同一项目中,同样,我在使用类库时遇到了困难。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句