简体   繁体   English

在Visual Studio 2013中使用EF6的MySQL连接器

[英]MySQL Connector with EF6 in Visual Studio 2013

I am attempting to connect to MySQL through a C# .NET web MVC application. 我试图通过C#.NET Web MVC应用程序连接到MySQL。

My issue is that, when I attempt to add an ADO.NET Entity Data Model, generated from Database, based on my MySQL connection, I get the following error message: 我的问题是,当我尝试根据我的MySQL连接添加从数据库生成的ADO.NET实体数据模型时,我收到以下错误消息:

Your project references the latest version of Entity Framework; 您的项目引用了最新版本的Entity Framework; however, an Entity Framework database provider compatible with this version could not be found for you data connection. 但是,无法为您的数据连接找到与此版本兼容的Entity Framework数据库提供程序。 Exit this wizard, install a compatible provider, and rebuild your project before performing this action 在执行此操作之前,请退出此向导,安装兼容的提供程序并重建项目

I'm running the following software, upgrades & add-ons: 我正在运行以下软件,升级和附件:

  • Visual Studio 2013 Visual Studio 2013
  • MySQL Server v5.6.21 MySQL Server v5.6.21
  • MySQL For Visual Studio v1.2.3 MySQL For Visual Studio v1.2.3
  • Connector/NET v6.9.4 Connector / NET v6.9.4

NuGet packages: NuGet包:

  • EntityFramework v6.1.1 EntityFramework v6.1.1
  • MySQL.Data v6.9.3 MySQL.Data v6.9.3
  • MySQL.Data.Entities v6.8.3.0 MySQL.Data.Entities v6.8.3.0
  • SQL.Web v6.9.3 SQL.Web v6.9.3

My web.config, entityFramework block: 我的web.config,entityFramework块:

    ..
</system.webServer>
<entityFramework>
  <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
  <providers>
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
  </providers>
</entityFramework>
<runtime>
    ..

I have looked at all other solutions I could find here on SO and generally through google, tried all of them and none of the solutions seems to have worked or have been for very different versions of Connector/NET or other of the programs needed. 我已经查看了我可以在这里找到的所有其他解决方案,通常是通过谷歌,尝试了所有这些解决方案似乎没有工作或已经用于非常不同版本的Connector / NET或其他所需的程序。

Can anyone spot what I am doing wrong? 谁能发现我做错了什么?

I had the same issue. 我遇到过同样的问题。 After installing: 安装后:

  • Visual Studio 2013 Community Edition Visual Studio 2013社区版
  • MySQL for Visual Studio 1.2.3 MySQL for Visual Studio 1.2.3
  • MySQL Connector .NET 6.9.5 MySQL Connector .NET 6.9.5

I didn't want to reinstall Visual Studio, so after some tests, I found that the folder " C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\PrivateAssemblies " contained old versions of the following files: 我不想重新安装Visual Studio,所以经过一些测试,我发现文件夹“ C:\\ Program Files(x86)\\ Microsoft Visual Studio 12.0 \\ Common7 \\ IDE \\ PrivateAssemblies ”包含以下文件的旧版本:

  • MySql.Data.dll MySql.Data.dll
  • MySql.Data.Entity.EF6.dll MySql.Data.Entity.EF6.dll
  • MySql.Web.dll MySql.Web.dll

After closing Visual Studio, I replaced these files with those in " C:\\Program Files (x86)\\MySQL\\MySQL Connector Net 6.9.5\\Assemblies\\v4.5 ", and now it works! 关闭Visual Studio后,我将这些文件替换为“ C:\\ Program Files(x86)\\ MySQL \\ MySQL Connector Net 6.9.5 \\ Assemblies \\ v4.5 ”中的文件,现在它可以工作了!

Note: replace 6.9.5 in the subfolder name above, with the actual version installed in your system. 注意:在上面的子文件夹名称中替换6.9.5,并在系统中安装实际版本。

This worked for me WITHOUT reinstalling the Visual Studio or anything what so ever. 这对我有用,无需重新安装Visual Studio或任何其他任何东西。

  • installed latest MySQL visual studio plugin and MySQL connector net 安装了最新的MySQL visual studio插件和MySQL连接器网
  • removed the entityFramework Tag in App.config or Web.config and all its child tags. 删除了App.config或Web.config中的entityFramework标记及其所有子标记。
  • replaced it with the following code: 以下代码替换它:
<entityFramework>
  <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
  <providers>
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
  • added MySql.Data.dll, MySql.Data.Entity.EF6.dll, and MySql.Web.dll(if not WPF app) references to the project. 添加了 MySql.Data.dll,MySql.Data.Entity.EF6.dll和MySql.Web.dll(如果不是WPF应用程序)对项目的引用。
  • rebuilt the project and then added the ado.net model. 重建项目,然后添加ado.net模型。

NOTE : Make sure you remove only the entityFramework tag and its children, and replace it with the snipped above. 注意 :确保仅删除entityFramework标记及其子标记,并将其替换为上面的剪切标记。 If you delete any "extra" tags you might have even bigger problems with your project, like I did. 如果删除任何“额外”标签,您的项目可能会遇到更大的问题,就像我一样。 X) X)

Basic solution is to reinstall MySQL and VisualStudio including Entity and .NET. 基本解决方案是重新安装MySQL和VisualStudio, 包括 Entity和.NET。

For some reason, I am not certain why, the installation process corrupts the DLLs, or at least that is the experience I had and other colleagues. 出于某种原因,我不确定为什么,安装过程会破坏DLL,或者至少那是我和其他同事的经历。 Our solution has been to try all of above with to no avail. 我们的解决方案一直尝试以上所有方面都无济于事。 So in the end we tried to reinstall, and it worked. 所以最后我们尝试重新安装,并且它有效。 So try that :) 所以尝试:)

Just add to web.config: 只需添加到web.config:

   <system.data>
    <DbProviderFactories>
      <clear />
      <add name="MySQL Data Provider"
           invariant="MySql.Data.MySqlClient"
           description=".Net Framework Data Provider for MySQL"
           type="MySql.Data.MySqlClient.MySqlClientFactory, 
                 MySql.Data" />
    </DbProviderFactories>
   </system.data>

Do not forget to rebuild the solution before attempt to add an ADO.NET Entity Data Model, generated from Database, again. 在尝试再次添加从Database生成的ADO.NET实体数据模型之前,请不要忘记重建解决方案

I've encountered the same problem. 我遇到了同样的问题。 Fortunately, I fixed it. 幸运的是,我修复了它。 The MySql.Data.dll or MySql.Data.Entity.EF6.dll is not compatible to EF 6.1 up version. MySql.Data.dll或MySql.Data.Entity.EF6.dll与EF 6.1 up版本不兼容。 What I did, I installed EF version 6.0.0.0 in nuget console by this command -> install-package entityframework -version 6.0.0.0 and it fixed the issue. 我做了什么,我通过这个命令 - > install-package entityframework -version 6.0.0.0在nuget控制台中安装了EF版本6.0.0.0并解决了这个问题。

Now this isn't an issue I've had myself, but I did find the following possible solution: 现在这不是我自己的问题,但我找到了以下可能的解决方案:

  1. Right click on the solution (top level in the solution explorer). 右键单击解决方案(解决方案资源管理器中的顶级)。
  2. Manage Nuget packages for solution. 管理Nuget包以获得解决方案。
  3. Go to Installed tab. 转到已安装选项卡。
  4. For all of the EntityFramework related packages (MySql.Data, MySql.Data.Entities, MySql.ConnectorNET.Entity and MySql.ConnectorNET.Data), select them then select the "Manage" button. 对于所有与EntityFramework相关的包(MySql.Data,MySql.Data.Entities,MySql.ConnectorNET.Entity和MySql.ConnectorNET.Data),选择它们,然后选择“管理”按钮。
  5. Enable each package for all projects. 为所有项目启用每个包。

What I noticed here was that he had two extra packages you haven't mentioned "MySql.ConnectorNET.Entity" and "MySql.ConnectorNet.Data". 我在这里注意到他有两个额外的包,你没有提到“MySql.ConnectorNET.Entity”和“MySql.ConnectorNet.Data”。 It could be that you are missing these and they are needed to work with Entity Framework 6.1.1 可能是您缺少这些并且需要使用Entity Framework 6.1.1

(Source: No Entity Framework Provider Found For ) (来源: 找不到实体框架提供商

If you need to use the EntityFramework with a mysql Database you just need to install an older version of EntityFramework like v 5.0 with the package manager console. 如果您需要将EntityFramework与mysql数据库一起使用,您只需要使用包管理器控制台安装较旧版本的EntityFramework,例如v 5.0。 Write Install-Package EntityFramework -Version 5.0.0. 编写Install-Package EntityFramework -Version 5.0.0。 Enjoy. 请享用。 https://www.nuget.org/packages/EntityFramework/5.0.0 https://www.nuget.org/packages/EntityFramework/5.0.0

I have a working Project with version 6.8.3 of MySQL Connector Libraries and EF6 v6.0.0. 我有一个工作项目与MySQL连接器库和EF6 v6.0.0版本6.8.3。 I remember this error, I think I solved adding some line to app.config manually. 我记得这个错误,我想我解决了手动向app.config添加一些行。

I list you the lines in my app.config related to EF6 and MySQL Configuration, are different to yours, maybe it helps you: 我列出了我的app.config中与EF6和MySQL配置相关的行,与你的不同,也许它可以帮到你:

<configSections>
    ...

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
</entityFramework>

I had installed MySQL Connector Net 6.8.3 with the package dowloaded from MySQL site, and added two references to the Project: MySql.Data and MySql.Data.Entity.EF6, both 6.8.3 versión (I notice you are using different versions of this files...). 我已经安装了MySQL Connector Net 6.8.3,并从MySQL站点下载了软件包,并添加了两个对Project的引用:MySql.Data和MySql.Data.Entity.EF6,两者都是6.8.3versión(我注意到你使用的是不同的版本)这些文件...)。

I added EF6 (v 6.0.0) to the project via Nuget. 我通过Nuget将EF6(v 6.0.0)添加到项目中。

Are not the latest versions, but hope this helps. 不是最新版本,但希望这会有所帮助。

Solution is to add this to app.config / web.config on the project your are trying to add/modify data model. 解决方案是将此添加到您尝试添加/修改数据模型的项目上的app.config / web.config

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

change version according to your connector. 根据您的连接器更改版本。

first of all, re-install vs 1.2.3, adding The following References to your project or check it versions: 首先,重新安装vs 1.2.3,将以下引用添加到您的项目或检查它的版本:

MySql.Data.dll MySql.Data.dll

MySql.Data.Entity.EF6.dll MySql.Data.Entity.EF6.dll

MySql.Web.dll MySql.Web.dll

Remove the default entityFramework tag in App.config or Web.config Rebuild your project, you can add ADO.NET Entity Data Model 删除App.config中的默认entityFramework标记或Web.config重建项目,可以添加ADO.NET实体数据模型

I was using visual studio 2013 community. 我当时正在使用visual studio 2013社区。 Wanted to do Code First on MySQL db. 想在MySQL db上做Code First。 I was having the same problem of "Your project references the latest version of Entity Framework;..." What solved my problem were simple steps given below. 我遇到了同样的问题“您的项目引用了最新版本的Entity Framework; ...”解决了我的问题的原因是下面给出的简单步骤。

  1. After adding a project, Use nuget console to add EntityFramework6.1.3, by install-package EntityFramework 添加项目后,使用nuget console通过install-package EntityFramework添加EntityFramework6.1.3
  2. User Console to add MySQL reference by MySQL.Data.Entity 用户控制台通过MySQL.Data.Entity添加MySQL引用
  3. Open your Web.config or App.config to change the entityframework and system.data sections as given below 打开Web.config或App.config以更改entityframework和system.data部分,如下所示

      <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer" /> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"> </provider> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="MySql.Data.MySqlClient" /> <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.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> 
  4. At this point make sure you rebuild your project. 此时,请确保重建项目。 (It's a must) (必须的)

  5. Now add ADO Entity Models and chose MySQL and it works. 现在添加ADO实体模型并选择MySQL,它可以工作。

By the way the plugins I use are M4VS 1.2.3 and MySQL .NET Connector 6.9.7 顺便说一句,我使用的插件是M4VS 1.2.3和MySQL .NET Connector 6.9.7

Quite simply I was using Nuget to reference the MySQL.dll (etc) and it automatically referenced the .net 4.0 dll's. 很简单,我使用Nuget引用MySQL.dll(等),它自动引用.net 4.0 dll。 I just changed the references to 4.5 and it worked. 我刚刚将引用更改为4.5并且它有效。

do in nuget command line 在nuget命令行中执行

update-package -reinstall -ignoreDependencies

and rebuild solution this work or me 并重建解决方案这项工作或我

I added this which solved it for me: 我添加了这个为我解决的问题:

 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

There are still problems in 2018... 2018年仍有问题......

DB-first scenario: for Visual Studio 2017 Community Edition I found working combination: DB-first场景:对于Visual Studio 2017社区版,我找到了工作组合:

  • MySQL for Visual Studio 1.2.8 MySQL for Visual Studio 1.2.8
  • MySQL Connector .NET 6.9.9 MySQL Connector .NET 6.9.9
  • EntityFramework 5.0.0 (I`ve tryed different combinations for v6.x with no luck) EntityFramework 5.0.0(我试过v6.x的不同组合没有运气)

Code-First scenario I can run using official documentation https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html with the latest Connector v.8.0.11 and EF6. Code-First场景我可以使用官方文档https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html与最新的Connector v.8.0.11和EF6一起运行。

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

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