Using MySQL & MSSQL for two different databases with Entity Framework - 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.
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>
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.
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" />
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?

To resolve this error i put this on my App.config: "codeConfigurationType"
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
Why? Possible the Configurations is not fiding the location of MySqlEFConfiguration.
Im using here only Mysql, i dont know if this work on SQLServer and Mysql togueter.
For your problem this link can be useful: Link
And you can have 2 separeted configuration.cs Files. One for MySql and other for MSSQL

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

I had the same issue. I'm using migrations in my code first project with two db contexts for mysql and mssql. The issue is because EF6 tries to find db configuration in config file or in the same assembly where db context is. For mysql migrations I use MySqlHistoryContext that inherits from DbContext. During db initialization stage EF6 creates instance of MySqlHistoryContext and find MySqlEFConfiguration type and tries to use it.
I found two ways to solve the issue. The first is disabling migrations checks by call Database.SetInitializer with null argument. The second is inherit from MySqlHistoryContext in my own assembly. The second seems to work for me.

I chased down all the solutions I could find on SO and elsewhere, and for me the issue 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 presented after we introduced MySQL for EF (in a different class library).
To be clear, in MSSQL the connection string we just changed "server" to "data source" and we changed "database" to "initial catalog".

Related

MySQL version 7.0.6-IR3 issue when try to connect to db

I got below error when i try to connect to MySQL db.
Inheritance security rules violated by type:
'MySql.Data.MySqlClient.MySqlProviderServices'. Derived types must
either match the security accessibility of the base type or be less
accessible.
Below is the entityframework config:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=7.0.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
Before this i'm using MySQL 6.9.9 and no error happen.
Please help.
It is a known bug in MySql connector, tracked here:
https://bugs.mysql.com/bug.php?id=89134
Workaround is to downgrade to lower version connector (like the one you were using)

Using MSSQL and MySQL database both as entity framework 6 model

I'm new to work with MySQL with asp.net and I'm trying to use asp.net mvc 4 and entity framework 6 to view datas from MySQL & MSSQL database both. So far MSSQL is working fine. But whenever I try to view datas from MySQL tables I get this error,
Schema specified is not valid. Errors: Models.MySqlModel.ssdl(2,2) : error
0152: No Entity Framework provider found for the ADO.NET provider with
invariant name 'MySql.Data.MySqlClient'. Make sure the provider is registered
in the 'entityFramework' section of the application config file. See
http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
I've installed MySQL for Visual Studio 1.1.4 & MySQL Connector .NET 6.8.3, I've added MySql.Data, MySql.Data.Entity.EF5, MySql.Data.Entity.EF6 assemblies in the reference folder. This is my Web.Config code for db connection,
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ABCoLtd-20150101142609;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ABCoLtd-20150101142609.mdf" />
<add name="mytestdbEntities" connectionString="metadata=res://*/Models.SampleModel.csdl|res://*/Models.SampleModel.ssdl|res://*/Models.SampleModel.msl;provider=System.Data.SqlClient;provider connection string="data source=DREAGLEASUS64\SQLEXPRESS;initial catalog=mytestdb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /><add name="mdsdata_demoEntities" connectionString="metadata=res://*/Models.MySqlModel.csdl|res://*/Models.MySqlModel.ssdl|res://*/Models.MySqlModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=4de570;database=mdsdata_demo"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
Code from Controller to view data from MySQL table,
namespace ABCoLtd.Controllers
{
public class HomeController : Controller
{
mytestdbEntities db = new mytestdbEntities(); //MSSQL Model
mdsdata_demoEntities dsedb = new mdsdata_demoEntities(); //MySQL Model
public ActionResult InstrumentList()
{
return View(dsedb.mkistats.ToList()); //The Line showing error
}
}
}
Am I doing something wrong here? I need this help badly. Please help me, your help will be really appreciated. Tnx.
UPDATE
When I see them in Reference Manager it says 6.8.3.0 but in Properties I get 6.8.4.0. Is it the possible bug? If it is, what is the solution? See the images below,
Check if you installed correct bit version and enable 32-bit applications in IIS.
And try to delete one of either references

MySql Connector with EF 6

I am having a weird issue with MySql Connector(6.8.3) and EF6. I was working on a WebApi project where I use MySql and EF6 with Database first approach. Everything worked fine[even deployed on one of the test servers] until I changed the database from 'Test' database to 'Production' database [just the database name] in the connection string and updated the model[just to see nothing is broken!]. After that, it failed to connect to database. So, I changed the connection string back and rebuilt the solution, then I got bunch of 'Mapping' warnings. I deleted the model and tried to create the model again from the database. Now, I am getting the following message
Your project references the latest version of Entity Framework;
however, an Entity Framework database provider compatible with this
version could not be found for you data connection. Exit this wizard,
install a compatible provider, and rebuild your project before
performing this action
Here is the config file
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.data>
<DbProviderFactories>
<clear/>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySql.Data.MySqlClient" description="ADO.Net driver for MySQL" invariant="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,Version=6.8.3.0, Culture=neutral,PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
<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>
I tried reinstalling the connector and EF from Nuget, but nothing changed. Can someone please let me know whats going on?
Thanks
I had a similar issue, I had the MySql connector referenced to my project with Nuget. But the server had another version of MySql connector installed in the machine itself. So the application gave priority to the MySql connector installed in GAC of server.
All you have to do is either
Uninstall the MySql connector in Server
or
Install suitable MySql connector (in your case v6.8.3) in server and uninstall the rest
or
change your provider type version to one that is installed in the server.
Bloody hell, had the same issue.
I'm pretty new to the .NET environment so, I don't know how the packages manager and all work but it seems that when I import the assemblies for the MySQL connector, it updates the "web.conf" file with the wrong parameters for the provider.
In my case, using EF6 and MySQL Connector for EF v6.8.3.0, the following worked for me :
<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>
</providers>
</entityFramework>
Hope it helps someone
i know what the problem is, you have to do the next steps :
1. Right click on the project
2. Manage NuGet Packages
3.remove the MySql.Data
4.now you can do what you wanted to do
5.after you finish, go back to Manage NuGet Packages and add MySql.Data.Entity
:)
good luck
nofar eliasi

No Entity Framework provider found for 'MySql.Data.MySqlClient' ADO.NET provider

I'm trying to use Entity Framework with MySQL and I get the above error. I have the latest MySQL connector installed.
The full error reads:
No Entity Framework provider found for 'MySql.Data.MySqlClient' ADO.NET provider. Make sure the provider is registered in the 'entityFramework' section of the application config file.
However, I can't find anything that suggests just how you register it in the 'entityFramework' section.
Some other posts (example) suggest adding the provider to the system.Data DbProviderFactories section like this:
<DbProviderFactories>
<add
name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,
Version=6.2.3.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
But that doesn't work because it claims that the invariant name is duplicated. And, if I actually iterate through the System.Data.Common.DbProviderFactories I can see the last one is the MySQL provider:
MySQL Data Provider
.Net Framework Data Provider for MySQL
MySql.Data.MySqlClient
MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d
So the provider is there, but EF refuses to use it. Any ideas?
My full config looks like this:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<system.data>
<!--<DbProviderFactories>
<add
name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,
Version=6.2.3.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>-->
</system.data>
<connectionStrings>
<add name="myContext" connectionString="server=****;User Id=****;password=****;Persist Security Info=True;database=myDb"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
in EF5 or less, all ok.
in EF6, you need to use mysql connector 6.8.x, and add DbConfigurationTypeAttribute to you DbContext:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DemoContext : DbContext{}
which MySqlEFConfiguration is in MySql.Data.Entity.EF6.dll in 6.8.x. Have a try!
You need to create this section in config (EF 5):
<entityFramework>
<!-- ... -->
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity" />
</providers>
</entityFramework>
Update: Use this definition for EF 6:
<entityFramework>
<!-- ... -->
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
Thanks to elcool.
I just had the same situation when trying to configure Visual Studio Professional 2017 environment with MySQL, ADO.NET (Database First) and EF6.
After going through hell with every connector/NET available, I got it to work with Connector/NET v6.9.10 and following the steps below.
Uninstall/remove "Connector/NET" and "MySQL for Visual Studio" if installed.
Install "MySQL for Visual Studio" v2.0.5 CTP (MySQL for Visual Studio).
Note: Install MySQL for Visual Studio before Connector/NET.
Install "Connector/NET" v6.9.10 (Connector/Net).
https://i.stack.imgur.com/XOT1I.jpg Note: I tried using Connector/NET v6.8, v6.10 and v8 first, but none of them worked Here you can find all Connector Versions and Compatibilities with Visual Studio IDEs, but so far this list is inaccurate.
Download and Install "EntityFramework" v6.2.0 through NuGet.
Add references to C:\Program Files (x86)\MySQL\Connector.NET 6.9.10\Assemblies\v4.5\MySql.Data.dll
and
C:\Program Files (x86)\MySQL\Connector.NET 6.9.10\Assemblies\v4.5\MySql.Data.Entity.EF6.dll.
Add MySQL EF6 provider info inside App.config under entity framework providers as follow:
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
Rebuild project.
And that was it. VS2017 was ready to go for me. Hope this works for everybody, as it did for me today.
References:
Can't Create Entity Data Model - using MySql and EF6
No Entity Framework provider found for 'MySql.Data.MySqlClient' ADO.NET provider
Just adding a summary of versions (since I saw you are trying 6.2 which is way too old)
For EF4, use Connector/NET 6.6.x (current GA is 6.6.6)
For EF5, use Connector/NET 6.7.x (current GA is 6.7.4) or Connector/NET 6.8.x (current GA is 6.8.3).
For EF6, use Connector/NET 6.8.x (current GA is 6.8.3).
I've updated from EntityFramework 5.0 to 6.1 and MySQL connector 6.8.3 and only had to add the attribute to get things going again. Before adding the attribute everything would compile fine but crash at runtime.
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DemoContext : DbContext{}
I tried every different combination of config string and setup but I finally figured it out. My solution has the source code in one project and tests in another. I was using Entity Framework 6.1.1 and had installed MySql Connector 6.8.3 and MySql for Visual Studio 1.2.3.
The problem was that I had NuGet managing the packages, but only had them included in the main project. The solution was to
Right click on the Solution (top level in the solution explorer)
Manage Nuget packages for solution
Go to Installed tab
For all of the EntityFramework related packages (MySql.Data, MySql.Data.Entities, MySql.ConnectorNET.Entity and MySql.ConnectorNET.Data), select them then select the "Manage" button.
Enable each package for all projects.
For future reference, here's the official guide on using MySQL Connector/Net with EF 6 which includes all the necessary steps (assemblies, configs, ...etc.):
Chapter 10 EF 6 Support
Install latest version from nuget
https://www.nuget.org/packages/MySql.Data.Entity/
In my case, it was a missing reference. When you upgrade to EntityFramework 6, you need to add a reference to the assembly
System.Data.Entity
I think it's because MySql.Data.Entity.EF6 inherits from a bunch of stuff in this assembly, that it did not for previous version of EF.
When your app.config is good and all your references seem fine, it's a solution worth checking.
Nevermind. I noticed that I had EF 6 installed. I removed it and tried EF 5 instead (which NuGet says is the latest stable version) and it started working.
Still, a more useful error message would have been nice!
May just MySql provider is installed in machine-config (e. g. by .net connector installer?
Also the default connection factory should be something like "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" i guess...

Entity Framework 5 with MySql - Connection string example needed for web.config

I just need an example with a default MySQL developer installation. I have this working fine with SQLExpress. Here's my SQL Express connection string:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
What should I put to make this work for MySQL
Take a look at these posts
Using MySQL with Entity Framework
Setting up .NET MVC3 with MySQL
Hope this helps