问题
- .Net Framework : 4.7.2
- MySql server installed: 8.0.12
- MySql connector/Net : 8.0.12
- Entity Framework: 6.2.0
- MySql.Data : 8.0.12
- MySql.Data.EntityFramework: 8.0.12
Web.config configuration
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlProviderServices,
MySql.Data.EntityFramework, Version=8.0.12.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="DefaultConnection"
connectionString="server=localhost;user=root;database=app;password=root;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices,
MySql.Data.EntityFramework, Version=8.0.12, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"/>
</providers>
While connecting to DB, getting exception like below:
The requested .Net Framework Data Provider's implementation does not have an Instance field of a System.Data.Common.DbProviderFactory derived type.
回答1:
The configuration information in <DbProviderFactories> is incorrect. According to the MySQL Connector/NET documentation it should be:
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.12.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
回答2:
OK so this is for all the devs in 2021 (Date: 2 Jan 2021)
This is Written thinking of all the New Devs out there if you Disagree on anything please comment, I merely have stated what worked with me with an intention to help
first, I will tell u my tech
Application Details:
- WPF Dekstop application on C#
- Framework is (.NetFramework 4.7.2)
- Database is MySQL
Installed in Windows 10 Machine :
- Visual Studio 2019 (16.8.2)
- MySQL [Server + workbench + Shell] (8.0.22)
- Connector / NET (8.0.22)
- MySQL for Visual Studio (1.2.9)
NuGet Packages Installed:
- Entity Framework (6.4.4)
- MySQL.Data (8.0.22)
- MySql.Data.EntityFramework (8.0.22)
Rules You Must Follow
You Must Have The above-Mentioned Packages and Softwares for It to work except MySQL Workbench
The Version of Connector/NET, MySQL.Data & MySql.Data.EntityFramework must be the same it doesn't matter what Version they are but all must be the same For Example, if its 6.10.9 then all three must be the same, but in the case of this example version MySql.Data.EntityFramework changes to MySql.Data.Entity as
MySql.Data.EntityFramework was not intended for the older version and the latter was used before (please refer to the second last comment as of this day by @DavidRomo)
Things That Worked for me
- I already had the Packages mentioned above but for some reason, they just wouldn't work for me so if it's the same for you, Delete Connector And Mysql for visual Studio and reinstall them again and this time from the website and this is what worked for me
This was in my App.config file, I do not know why the versions are written incorrect in this file, but this came preinstalled/prewritten like that, also I had tried manually changing the version to the exact correct version looking at some videos but for me it didn't work
<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> ENTITY FRAMEWORK <entityFramework> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.22.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider> </providers> </entityFramework>```It doesn't matter if its a x64 (a 64 bit software) or a x86 (a 32 bit software) MSI package when Installing From the MySQL website
Don't care about the dependency packages that come pre-installed with it (like Bouncy castle)
The point it started working 'FOR ME' was when I did not update the dependency packages, I let it install and did not mind
I Tried Importing the Dll's that come with the connector but it didn't work for me at least ; EF6 still kept crashing after selecting the DB first approach, but I also didn't not import the other Dll's in File as well, like the Dll for Bouncy Castle and many others, so i do not know if that is a factor; So I removed the Imported Packages and Installed them from Nuget Package Manager with the dependencies
this is the File Location If You Were Wondering but still this did not work ' FOR ME', meaning it might work for you if you tried the right way
File Location : C:\Program Files (x86)\MySQL\MySQL Connector Net 8.0.22\Assemblies
I personally Installed the "Connector/Net" and "MySQL for Visual Studio" MSI packages Manually from the Mysql Website, I did not Use the Installer, I have although Tried With it and it Kept Installing one version less from the latest Package
This has taken a lot of my time hope you guys don't have to deal with the same thing
来源:https://stackoverflow.com/questions/51999120/mysql-entity-framework-exception