VS 2017中的MySQL和MVC实体框架无法正常工作 [英] MySQL and MVC Entity Framework in VS 2017 Not Working

查看:69
本文介绍了VS 2017中的MySQL和MVC实体框架无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动MVC EF Visual Studio 2017项目.我已经在本地实例上使用MySQL设置了所有的数据连接,但是当我创建ADO.net数据模型时,出现了以下错误:

I am trying to start an MVC EF Visual Studio 2017 project. I have my Data Connection all set up with MySQL on my local instance but when I go to create an ADO.net Data Model I get the error seen in the picture:

这里还有另一篇文章:不能使用适用于VS 2012和2013但不涵盖2017的实体框架6的MySQL连接.这是MySQL文档,说明哪些版本适用于2017:

There was another article here: Can't use a MySQL connection for entity framework 6 that covered VS 2012 and 2013 but not 2017. Here is the MySQL documentation that says what versions work with 2017: https://dev.mysql.com/doc/visual-studio/en/visual-studio-install.html

我正在使用:

MySQL Connector Net 6.9.9 |适用于Visual Studio 1.2.7的MySQL | MySQL服务器5.7

MySQL Connector Net 6.9.9 | MySQL for Visual Studio 1.2.7 | MySQL Server 5.7

MySQL.Data 6.9.9 | MySQL.Data.Entity 6.9.9 |实体框架6.1.3

MySQL.Data 6.9.9 | MySQL.Data.Entity 6.9.9 | Entity Framework 6.1.3

所有列出的内容均已通过MySQL测试并可以正常工作.我今天刚刚安装了所有新产品,因此没有出色的旧版本.我三重检查了;)

All of which are listed as tested and working by MySQL. I just installed all new everything today so there are no outstanding old versions. I triple checked ;)

连接字符串:

<connectionStrings> <add name="MySQL" connectionString="server=localhost;port=3306;user id=root;password=password;database=localdb" providerName="MySql.Data.MySqlClient" /> </connectionStrings>

<connectionStrings> <add name="MySQL" connectionString="server=localhost;port=3306;user id=root;password=password;database=localdb" providerName="MySql.Data.MySqlClient" /> </connectionStrings>

修改

我已找到本文 https://讨论建立连接的dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html ,但现在不是该错误,而是在我点击对话框后就消失了下一个".

I have found this article https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html that talks about setting up the connection but now instead of that error, the dialog box just goes away as soon as I hit 'Next'.

这是我做错了什么还是MySQL连接器损坏了吗?

Is this just me doing something incorrectly or a broken MySQL connector?

提前感谢您的任何建议!

Thanks in advance for any advice!

推荐答案

我要做的就是重新安装用于Visual Studio 2.0.5的MySQL ,然后完全删除并安装

What I had to do was reinstall MySQL for Visual Studio 2.0.5, then completely remove and install MySQL Connector 6.9.9. Seems the order does matter. After that, I completely remove these packages, and reinstalled in this exact order:

(随后重新启动Visual Studio)

EntityFramework 6.1.3 (I tried earlier versions and they don't work, so beware)
Mysql.Data 6.9.9
Mysql.Data.Entity 6.9.9 (NOT Mysql.Data.Entities!!! [for 6.9.9])
Mysql.Web 6.9.9

然后,确保您的web.config文件中包含以下内容:

Then, make sure the following is in your web.config file:

(请确保在编辑后重新启动)

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

(您可能需要注释掉任何现有的部分,并用这一部分替换)

(you may have to comment out any existing section and replace it with this one)

我不得不在此处重新提醒自己. 那部分是我忘记的(太长了),这导致了问题截图中的错误.

The last step I had to remind myself by revisiting here. That part is what I forgot about (been too long), which caused the error in the screenshot in the question.

最后一件事,您可能将错误的版本复制到了安装Visual Studio的位置;例如:

One last thing, you may have incorrect versions copied to where Visual Studio is installed; for example:

C:\Program Files (x86)\Microsoft Visual Studio\ {年份} \ {社区|企业|专业} \Common7\IDE\PrivateAssemblies

C:\Program Files (x86)\Microsoft Visual Studio {您的版本号} \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

(可能必须先关闭Visual Studio)

您可以选择每个文件,然后转到文件属性下的Details选项卡,以查看具有的版本.

You can select each file and go to the Details tab under the file properties to see what versions you have.

从此处(或在任何安装位置)获取新文件: C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies\v4.5 (这假定使用框架v4.5的Connector v6.9.9 [请参见项目属性的Application->Target Framework来确认您的设置]).

Get the new files from here (or wherever you installed it): C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies\v4.5 (this assumes Connector v6.9.9 using framework v4.5 [see your project properties' Application->Target Framework to confirm your setting]).

注意1:当您安装MySQL for Visual Studio时,它将更新PrivateAssemblies文件夹中的文件(请参见上面的Visual Studio路径),因此请仔细检查上面的程序集,以确保它们没有被更改为目标版本以外的任何内容(在本例中为6.9.9).不管NuGet所安装的是什么,Visual Studio都不会关心它,并且会查看私有程序集(我认为实际上是在启动过程中).

Note 1: When you install MySQL for Visual Studio, it updates the files in the PrivateAssemblies folder (see the Visual Studio paths above), so PLEASE double check the assemblies above to make sure they didn't get changed to anything other than your target version (6.9.9 in this case). Regardless of what NuGet installs, Visual Studio won't even care, and will look in the private assemblies (I think during startup actually).

注意2:如果收到"IsPrimaryKey"错误,请请参见此处.

Note 2: If you get an "IsPrimaryKey" error, see here.

我已完成的App.Config,可作为参考(与您进行比较):

My completed App.Config that works for reference (to compare with yours):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <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, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <connectionStrings>
    <!-- Connections Strings Go Here -->
  </connectionStrings>
  <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.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <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.9.9.0" newVersion="6.9.9.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

这篇关于VS 2017中的MySQL和MVC实体框架无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆