MySQL连接器6.7.4和实体框架5例外 [英] MySQL connector 6.7.4 and Entity Framework 5 exceptions
问题描述
我下载了 MySQL Connector / Net 6.7.4 和 MySQL for Visual Studio 1.0.2 ,然后按照这些说明进行测试:
- 创建与现有MySQL数据库的连接。
- 创建一个控制台应用程序。
- 从现有数据库连接添加ADO.NET实体数据模型。
- 编写一些从数据库中检索记录的代码。 >
<添加代码生成项目EF 5.x DbContext生成器,替换.tt文件。
运行应用程序,我收到了这个例外:
ConfigurationErrorsException :找不到或加载注册的.Net框架数据提供程序。
然后我添加了引用至 我的.NET 4.5项目中的$ code> MySql.Data 和 MySql.Data.Entity
库版本6.7.4.0。现在当我运行应用程序时,我得到一个不同的例外:
FileLoadException :无法加载文件或程序集'MySql.Data,Version = 6.6.5.0,culture = neutral,PublicKeyToken = c5687fc88969c44d'或其依赖关系之一。找到的程序集的清单定义与程序集引用不匹配。 (来自HRESULT的异常:0x80131040)
请注意版本号,这不是我安装的MySQL连接器的版本。 p>
如何使其正常工作?
这是:
- 添加对
MySql.Data
和MySql.Data.Entity
正确版本的库(对我来说是6.7.4.0,对于.NET 4.5)。 - 编辑
machine.config
,您的编辑器以管理员身份运行,并将所有出现的MySQL版本6.6.5.0
替换为6.7.4.0
。
对于第二步,请注意有多个 machine.config
文件,每个框架版本(3.0,3.5,4.0)和架构(32位,64位)。另请注意,.NET 4.5的 machine.config
文件位于.NET 4.0文件夹中。您可以在以下位置找到 machine.config
文件:
C:\Windows \Microsoft.NET\Framework\\Config
And:
C:\Windows\Microsoft.NET\Framework64\\Config
如果在 machine.config
文件中没有引用MySQL,则可能没有安装 MySQL for Visual Studio 。要么这样做,要么将以下内容添加到项目的 app.config
文件中:
code>< system.data>
< DbProviderFactories>
< add name =MySQL数据提供者
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 for Visual Studio
和将上述代码片段添加到您的 app.config
文件中,然后您将收到以下异常:
ConfigurationErrorsException :列InvariantName被限制为唯一的。值'MySql.Data.MySqlClient'已经存在。
I downloaded MySQL Connector/Net 6.7.4 and MySQL for Visual Studio 1.0.2, and then followed these instructions to test it:
- 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: Failed to find or load the registered .Net Framework Data Provider.
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: 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)
Note the version number, which is not the version of MySQL Connector that I have installed.
How do I get it working correctly?
The trick to solving this was:
- Add references to the
MySql.Data
andMySql.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 version6.6.5.0
by6.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). 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\Framework\\Config
And:
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. 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>
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: Column 'InvariantName' is constrained to be unique. Value 'MySql.Data.MySqlClient' is already present.
这篇关于MySQL连接器6.7.4和实体框架5例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!