Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL and MVC Entity Framework in VS 2017 Not Working

Tags:

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:

enter image description here

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!

like image 605
Alex Avatar asked Aug 02 '17 19:08

Alex


People also ask

How connect MySQL to MVC?

Installing and adding reference of MySql ConnectorRight Click the Project in Solution Explorer and click Manage NuGet Packages from the Context Menu. 2. Now you will need to look for MySql. Data package and once found, you need to click the Install Button.

Is Entity Framework compatible with MySQL?

MySQL Connector/NET integrates support for Entity Framework 6 (EF6), which now includes support for cross-platform application deployment with the EF 6.4 version.

Does Visual Studio 2022 support MySQL?

MySQL isn't supported for Visual Studio 2022 yet. it's weird and frustrating, to be honest, but if you want to work with MySQL with VS22, you either need to change the Database or go back to VS19.


1 Answers

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>
like image 136
James Wilkins Avatar answered Oct 21 '22 01:10

James Wilkins