Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 767
Matt Burland Avatar asked Feb 28 '13 18:02

Matt Burland


2 Answers

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!

like image 162
AlexanderYao Avatar answered Oct 05 '22 01:10

AlexanderYao


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.

like image 23
Der_Meister Avatar answered Oct 04 '22 23:10

Der_Meister