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

2023-09-02 10:38:39 作者:苦笑流年记忆

我下载的MySQL Connector /网6.7.4 和的 MySQL的为Visual Studio 1.0.2 ,然后跟着的这些说明来测试它:

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数据库的连接。 创建一个控制台应用程序。 添加的 ADO.NET实体数据模型的从现有的数据库连接。 添加code代项目的 EF 5.x中的DbContext发电机的,取代了.TT文件。 在写一些检索的记录从数据库中code。 Create a connection to the existing MySQL database. Create a console application. Add the ADO.NET Entity Data Model from the existing database connection. Add Code Generation Item EF 5.x DbContext Generator, replacing the .tt files. Write some code that retrieved records from the database.

运行应用程序,我得到这个异​​常:

Running the application, I got this exception:

ConfigurationErrorsException :无法找到或加载已注册的.NET Framework数据提供

ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.

然后,我添加引用到 MySql.Data 和 MySql.Data.Entity 库6.7.4.0版本我的.NET 4.5的项目。现在,当我运行的应用程序,我得到一个不同的异常:

Then I added references to the MySql.Data and MySql.Data.Entity libraries version 6.7.4.0 to my .NET 4.5 project. Now when I run the application, I get a different exception:

FileLoadException :无法加载文件或程序集MySql.Data,版本= 6.6.5.0,文化=中性公钥= c5687fc88969c44d或它的某一个依赖。找到的程序集清单定义不匹配的程序集引用。 (从HRESULT异常:0x80131040)

FileLoadException: Could not load file or assembly 'MySql.Data, Version=6.6.5.0,culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

请注意的版本号,这是不是我已经安装了MySQL Connector的版本。

Note the version number, which is not the version of MySQL Connector that I have installed.

我如何得到它的正常工作?

How do I get it working correctly?

推荐答案

诀窍解决,这是:

添加引用的正确版本(6.7 MySql.Data 和 MySql.Data.Entity 库。 4.0 .NET 4.5,在我的情况),以该项目。 编辑 machine.config的与你的编辑器以管理员身份运行,及取代MySQL版本所有出现的 6.6.5.0 通过 6.7.4.0 。 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. 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的文件,每个框架的版本(3.0,3.5,4.0)和架构(32位, 64位)。还要注意的是 machine.config的文件.NET 4.5是.NET 4.0文件夹。你可以找到 machine.config的文件:

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). Also note that the machine.config file for .NET 4.5 is in the .NET 4.0 folder. You can find the machine.config files in:

C: WINDOWS Microsoft.NET 框架\配置

精通MySQL之架构篇

C:WindowsMicrosoft.NETFramework\Config

C: WINDOWS Microsoft.NET Framework64 \配置

C:WindowsMicrosoft.NETFramework64\Config

如果有在 machine.config的文件到MySQL没有提及,你可能没有安装的 MySQL的为Visual Studio 。要么做到这一点,或将以下添加到您的项目的的app.config 文件:

If there are no references to MySQL in the machine.config file, you might not have installed MySQL for Visual Studio. Either do that, or add the following to the app.config file of your project:

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

但是请注意,当你两个安装的MySQL的Visual Studio 的和的添加上面的代码到你的应用程序。配置文件,然后你会得到这个异​​常:

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:

ConfigurationErrorsException :列InvariantName'约束是唯一的。值'MySql.Data.MySqlClient'已经是present。

ConfigurationErrorsException: Column 'InvariantName' is constrained to be unique. Value 'MySql.Data.MySqlClient' is already present.