找不到“MySql.Data.MySqlClient"ADO.NET 提供程序的实体框架提供程序

2021-11-20 00:00:00 mysql entity-framework

我正在尝试将 Entity Framework 与 MySQL 一起使用,但出现上述错误.我安装了最新的 MySQL 连接器.

完整错误如下:

未找到MySql.Data.MySqlClient"ADO.NET 提供程序的实体框架提供程序.确保提供程序已在应用程序配置文件的entityFramework"部分注册.

但是,我找不到任何表明您如何在entityFramework"部分注册它的信息.

其他一些帖子(示例) 建议将提供程序添加到 system.Data DbProviderFactories 部分,如下所示:

<添加名称="MySQL 数据提供程序"不变="MySql.Data.MySqlClient"description="MySQL 的.Net Framework 数据提供程序"type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,版本=6.2.3.0,文化=中性,PublicKeyToken=c5687fc88969c44d"/></DbProviderFactories>

但这不起作用,因为它声称 invariant 名称是重复的.而且,如果我真的遍历 System.Data.Common.DbProviderFactories,我可以看到最后一个是 MySQL 提供程序:

MySQL 数据提供程序用于 MySQL 的 .Net Framework 数据提供程序MySql.Data.MySqlClientMySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d

所以提供者在那里,但EF拒绝使用它.有什么想法吗?

我的完整配置如下:

<预><代码><配置><configSections><!-- 有关实体框架配置的详细信息,请访问 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><系统数据><!--<DbProviderFactories><添加名称="MySQL 数据提供程序"不变="MySql.Data.MySqlClient"description="MySQL 的.Net Framework 数据提供程序"type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,版本=6.2.3.0,文化=中性,PublicKeyToken=c5687fc88969c44d"/></DbProviderFactories>--></system.data><连接字符串><add name="myContext" connectionString="server=****;User Id=****;password=****;Persist Security Info=True;database=myDb"providerName="MySql.Data.MySqlClient"/></connectionStrings><启动><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></启动><实体框架><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><参数><参数值="v11.0"/></参数></defaultConnectionFactory></entityFramework></配置>

解决方案

没关系.我注意到我安装了 EF 6.我删除了它并尝试使用 EF 5(NuGet 说这是最新的稳定版本),它开始工作了.

不过,如果有更有用的错误消息就好了!

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>

解决方案

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!

相关文章