简体   繁体   中英

MySQL and MVC Entity Framework in VS 2017 Not Working

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:

在此输入图像描述

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

I am using:

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 | Entity Framework 6.1.3

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

Connection string:

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


Edit

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


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

Thanks in advance for any advice!

So eventually what I did was I created a new project and did a clean install of MySQL.Data 6.9.9 | MySQL.Data.Entity 6.9.9 | Entity Framework 6.1.3 and then tried to make the data model again.

It almost worked! I got an error saying: 'System.Data.StrongTypingException: The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull. ---> System.InvalidCastException: Specified cast is not valid. 'System.Data.StrongTypingException: The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull. ---> System.InvalidCastException: Specified cast is not valid. But lots of people have worked past this issue by going into the MySQL command line and executing: set global optimizer_switch='derived_merge=off' You can find this bug here: https://bugs.mysql.com/bug.php?id=79163

For whatever reason this works. here is the output of my command line to as to help any newbies at MySQL:

在此输入图像描述

This fixes EVERYTHING! hopefully someone finds this an doesn't waste as much time as I did.

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:

(restart Visual Studio afterward)

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

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

(restart after editing to be sure)

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

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

C:\\Program Files (x86)\\Microsoft Visual Studio\\ {Year} \\ {Community|Enterprise|Professional} \\Common7\\IDE\\PrivateAssemblies

or

C:\\Program Files (x86)\\Microsoft Visual Studio {Your Version Number} \\Common7\\IDE\\PrivateAssemblies

MySql.Data.dll

MySql.Data.Entity.EF6.dll

MySql.Web.dll

(may have to close Visual Studio first)

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

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]).

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

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

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>

@James Wilkins Thanks for the detailed answer, I've to add, sometime when you are adding Mysql.data or MySQL.data.entity the assembly version doesn't match the assemblies installed by MySqlConnector/Net, as we often download the latest version from Nuget, this cause this issue again, so what I did and fixed the issue, I copied the dll (Mysql.data, MySQL.data.entity and web) from

C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.9\Assemblies\v4.5

to my application bin/debug folder and reference these instead of downloading from Nugets. This fixed my issues. You may also need to update the assembly version in app.config or web.config and packages.config file as well.

<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"> 

To

<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">

And update the version for the entries here in packages.config

  <package id="MySql.Data" version="6.9.9" targetFramework="net452" />
  <package id="MySql.Data.Entity" version="6.9.9" targetFramework="net452" />
  <package id="MySql.Web" version="6.9.9" targetFramework="net452" />

Tuning in to confirm that I got everything working -including the db-first gui-tool-chain of Entity Framework in VS2017- on a .net 4.7.1 asp.net-mvc5 wev-solution by employing these exact components on Visual Studio 2017 (VS2017 => ver15.5.7 aka fully updated VS2017 at the time of this writing):

Entity Framework ver6.2.0 [nuget]
MySql.Data ver6.9.11 [nuget]
MySql.Data.Entity ver6.9.11 [nuget]
MySql.Web ver6.9.11 [nuget]
MySQL for Visual Studio ver2.0.5 [system-wide msi installer]
MySql-Connector ver6.9.11 [system-wide msi installer]

I tried using the latest MySql-Connector (ver.6.10.x) but it turned out that the gui tools of VS2017 for the db-first approach wouldn't work at all. I guess MySql-Connector has to be aligned with the rest of the dlls version-wise.

Web.config looks like so:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    [...]
  </configSections>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True;" />
      </parameters>
    </defaultConnectionFactory>
    [...]
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      [...]
    </providers>
    [...]
  </entityFramework>
  [...]
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add description=".Net Framework Data Provider for MySQL" invariant="MySql.Data.MySqlClient" name="MySQL Data Provider" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
    </DbProviderFactories>
  </system.data>
  [...]
</configuration>

And the App.config the child project hosting the .edmx file looks like so:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework" 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" />
      [...]
    </providers>
  </entityFramework>
  [...]
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add description=".Net Framework Data Provider for MySQL" invariant="MySql.Data.MySqlClient" name="MySQL Data Provider" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
    </DbProviderFactories>
  </system.data>
  [...]
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
  </startup>
</configuration>

And the packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.2.0" targetFramework="net471" allowedVersions="[6.2.0]" />
  <package id="LinqKit" version="1.1.13" targetFramework="net471" allowedVersions="[1.1.13]" />
  <package id="MySql.Data" version="6.9.11" targetFramework="net471" allowedVersions="[6.9.11]" />
  <package id="MySql.Data.Entity" version="6.9.11" targetFramework="net471" allowedVersions="[6.9.11]" />
  <package id="MySql.Web" version="6.9.11" targetFramework="net471" allowedVersions="[6.9.11]" />
</packages>

Note: The packages.config has been tweaked intentionally by means of [allowedVersions] to forbid unintentional auto-upgrades of MySql packages to their more recent versions. This was done due to dodge issues that plague the 6.10.x flavors of the MySql nugets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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