简体   繁体   English

MySQL 连接器 6.7.4 和实体框架 5 异常

[英]MySQL connector 6.7.4 and Entity Framework 5 exceptions

I downloaded MySQL Connector/Net 6.7.4 and MySQL for Visual Studio 1.0.2 , and then followed these instructions to test it:我下载了MySQL Connector/Net 6.7.4MySQL for Visual Studio 1.0.2 ,然后按照以下说明进行测试:

  1. Create a connection to the existing MySQL database.创建与现有 MySQL 数据库的连接。
  2. Create a console application.创建控制台应用程序。
  3. Add the ADO.NET Entity Data Model from the existing database connection.从现有数据库连接添加ADO.NET 实体数据模型
  4. Add Code Generation Item EF 5.x DbContext Generator , replacing the .tt files.添加代码生成项EF 5.x DbContext Generator ,替换 .tt 文件。
  5. Write some code that retrieved records from the database.编写一些从数据库中检索记录的代码。

Running the application, I got this exception:运行应用程序,我得到了这个异常:

ConfigurationErrorsException : Failed to find or load the registered .Net Framework Data Provider. ConfigurationErrorsException :无法找到或加载已注册的 .Net Framework 数据提供程序。

Then I added references to the MySql.Data and MySql.Data.Entity libraries version 6.7.4.0 to my .NET 4.5 project.然后我将MySql.DataMySql.Data.Entity库版本 6.7.4.0 的引用添加到我的 .NET 4.5 项目中。 Now when I run the application, I get a different exception:现在,当我运行应用程序时,我得到了一个不同的异常:

FileLoadException : Could not load file or assembly 'MySql.Data, Version=6.6.5.0,culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. FileLoadException :无法加载文件或程序集“MySql.Data,版本=6.6.5.0,文化=中性,PublicKeyToken=c5687fc88969c44d”或其依赖项之一。 The located assembly's manifest definition does not match the assembly reference.定位的程序集的清单定义与程序集引用不匹配。 (Exception from HRESULT: 0x80131040) (来自 HRESULT 的异常:0x80131040)

Note the version number, which is not the version of MySQL Connector that I have installed.注意版本号,它不是我安装的 MySQL Connector 的版本。

How do I get it working correctly?我如何让它正常工作?

The trick to solving this was:解决这个问题的诀窍是:

  1. Add references to the MySql.Data and MySql.Data.Entity libraries of the correct version (6.7.4.0 for .NET 4.5, in my case) to the project.向项目添加对正确版本的MySql.DataMySql.Data.Entity库的引用(在我的情况下,.NET 4.5 为 6.7.4.0)。
  2. Edit machine.config with your editor run as administrator, and replace all occurences of MySQL version 6.6.5.0 by 6.7.4.0 .使用以管理员身份运行的编辑器编辑machine.config ,并将所有出现的 MySQL 版本6.6.5.06.7.4.0

For the second step, note that there are multiple machine.config files, one for each framework version (3.0, 3.5, 4.0) and architecture (32-bit, 64-bit).对于第二步,请注意有多个machine.config文件,每个框架版本(3.0、3.5、4.0)和架构(32 位、64 位)一个。 Also note that the machine.config file for .NET 4.5 is in the .NET 4.0 folder.另请注意,.NET 4.5 的machine.config文件位于 .NET 4.0 文件夹中。 You can find the machine.config files in:您可以在以下位置找到machine.config文件:

C:\\Windows\\Microsoft.NET\\Framework\\\\Config C:\\Windows\\Microsoft.NET\\Framework\\Config

And:和:

C:\\Windows\\Microsoft.NET\\Framework64\\\\Config C:\\Windows\\Microsoft.NET\\Framework64\\\\Config

If there are no references to MySQL in the machine.config file, you might not have installed MySQL for Visual Studio .如果machine.config文件中没有对 MySQL 的引用,则您可能没有为 Visual Studio安装MySQL Either do that, or add the following to the app.config file of your project:要么这样做,要么将以下内容添加到项目的app.config文件中:

<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.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

Note however, that when you both install MySQL for Visual Studio and add the above snippet to your app.config file, then you'll get this exception:但是请注意,当你都安装MySQL for Visual Studio上面的代码添加到您app.config文件,然后你会得到这个异常:

ConfigurationErrorsException : Column 'InvariantName' is constrained to be unique. ConfigurationErrorsException :列“InvariantName”被限制为唯一。 Value 'MySql.Data.MySqlClient' is already present.值 'MySql.Data.MySqlClient' 已经存在。

I don't like to edit machine.config.我不喜欢编辑machine.config。 Just add this redirect to web.config:只需将此重定向添加到 web.config:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-6.6.5.0" newVersion="6.7.4.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Using this should stop the exception that Virtlink mentioned:使用它应该停止 Virtlink 提到的异常:

<system.data>
    <DbProviderFactories>
        <remove name="MySQL Data Provider" />
        <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.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>

Especially note the <remove name="MySQL Data Provider" /> line.特别注意<remove name="MySQL Data Provider" />行。

Download MYSQL 6.7.4.0 from HERE .这里下载 MYSQL 6.7.4.0。 Notice that your specific problem requires 6.7.4.0 and NOT other version !请注意,您的特定问题需要 6.7.4.0 而不是其他版本!

Direct link is this .直接链接是这个

Download and add the files to the reference folder of your solution .下载文件并将其添加到解决方案的参考文件夹中。

This would probably solve your problem (it did for me , and yes , I know this can by a very annoying problem) .这可能会解决您的问题(对我来说确实如此,是的,我知道这是一个非常烦人的问题)。

Good luck :)祝你好运 :)

I realise this thread had an answer back in 2013 but I have just encountered this problem again.我意识到这个线程在 2013 年有一个答案,但我刚刚再次遇到这个问题。 In my situation, I recently installed the Windows 10 Anniversary update.在我的情况下,我最近安装了 Windows 10 周年更新。 Didn't have an issue before this.在此之前没有问题。

As per the answers above, it turns out my Machine.config file had been overwritten (I presume by the update).根据上面的答案,事实证明我的 Machine.config 文件已被覆盖(我认为是更新)。

For me once I restored the MySql Assembly information to the Machine.config file, it immediately started working again.对我来说,一旦我将 MySql 程序集信息恢复到 Machine.config 文件,它就会立即重新开始工作。

In particular the "Runtime" and "DbProviderFactories" sections had been wiped and had to be replaced.特别是“运行时”和“DbProviderFactories”部分已被擦除并且必须被替换。 They were as follows (these will vary depending on the versions of the assemblies you are using):它们如下(这些将根据您使用的程序集版本而有所不同):

Runtime Section:运行时部分:

 <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> <bindingRedirect oldVersion="6.7.4.0" newVersion="6.9.9.0" /> </dependentAssembly> <dependentAssembly xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity name="MySql.Data.Entity" publicKeyToken="c5687fc88969c44d" culture="neutral" /> <bindingRedirect oldVersion="6.7.4.0" newVersion="6.9.9.0" /> </dependentAssembly> <dependentAssembly xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity name="MySql.Web" publicKeyToken="c5687fc88969c44d" culture="neutral" /> <bindingRedirect oldVersion="6.7.4.0" newVersion="6.9.9.0" /> </dependentAssembly> </assemblyBinding> </runtime>

DbProviderFactories Section DbProviderFactories 部分

<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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories>

Hope this helps anyone else coming across the same problem.希望这可以帮助其他遇到同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM