Using MySQL and MSSQL for two different databases with Entity Framework

I am trying to create a web api that serves data from a MySQL database or an MSSQL database (completely different data and schemas). It also provides some endpoints for moving data between them. I created two class libraries to store EF models for both databases and successfully connected to both instances and ran queries against both. Where he falls, he tries to gain access to both. I have to configure the entityFramework section in web.config to make it work, I cannot get it so that both work at the same time efficiently. I am using EF6 and the latest MySQL connector.

This is the working configuration for MSSQL objects:

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

This is an error that occurs when trying 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 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. , , : EF MySql SqlServer?

- ?

+4
4

, , - MySQL 6.9.1. .

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>

, - .

+1

, App.config: "codeConfigurationType"

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

? , MySqlEFConfiguration.

Im, Mysql, , , SQLServer Mysql.

:

2 separeted configuration.cs. MySql MSSQL

+3

. db mysql mssql. , EF6 db , db. mysql MySqlHistoryContext, DbContext. db EF6 MySqlHistoryContext MySqlEFConfiguration . . - Database.SetInitializer . MySqlHistoryContext . , , .

0

I pursued all the solutions that I could find in SO and elsewhere, and for me the problem turned out to be my MSSQL connection string.

before:

<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Server=serverName/instance;Database=PublicWebsite;Integrated Security=True;" />

after

<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=serverName/instance;Initial Catalog=PublicWebsite;Integrated Security=True;" />

The "before" connection string worked fine for EF. The problem was introduced after we introduced MySQL for EF (in another class library).

To be clear, in MSSQL, the connection string we just changed the “server” to “data source”, and we changed the “database” to the “start directory”.

0
source

Source: https://habr.com/ru/post/1539540/


All Articles