MySQL connector 6.7.4 and Entity Framework 5 exceptions

19,801

Solution 1

The trick to solving this was:

  1. Add references to the MySql.Data and MySql.Data.Entity libraries of the correct version (6.7.4.0 for .NET 4.5, in my case) to the project.
  2. Edit machine.config with your editor run as administrator, and replace all occurences of MySQL version 6.6.5.0 by 6.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.

Solution 2

I don't like to edit machine.config. Just add this redirect to web.config:

<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.6.5.0" newVersion="6.7.4.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Solution 3

Using this should stop the exception that Virtlink mentioned:

<system.data>
    <DbProviderFactories>
        <remove name="MySQL Data Provider" />
        <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>

Especially note the <remove name="MySQL Data Provider" /> line.

Solution 4

Download MYSQL 6.7.4.0 from HERE . Notice that your specific problem requires 6.7.4.0 and NOT other version !

Direct link is this .

Download and add the files to the reference folder of your solution .

This would probably solve your problem (it did for me , and yes , I know this can by a very annoying problem) .

Good luck :)

Share:
19,801
Daniel A.A. Pelsmaeker
Author by

Daniel A.A. Pelsmaeker

When I was five years old, I wanted to learn how to read. Afraid that I might get bored in class if they'd allow that, my father decided to teach me how to play chess and my mother showed me how to get our Commodore 64 to repeat my name over and over. 10 PRINT "DANIEL" 20 GOTO 10 I was much more interested in the Commodore than in chess and from Commodore Basic through QBasic, Visual Basic I worked my way up to C#. This was all self-taught until I decided to follow an education in Computer Science after getting my Building Sciences degree. I have a bachelor's degree in Building Sciences (BBE) and master's degree in Computer Science (MSc), and I am currently working on getting my PhD.

Updated on July 27, 2022

Comments

  • Daniel A.A. Pelsmaeker
    Daniel A.A. Pelsmaeker almost 2 years

    I downloaded MySQL Connector/Net 6.7.4 and MySQL for Visual Studio 1.0.2, and then followed these instructions to test it:

    1. Create a connection to the existing MySQL database.
    2. Create a console application.
    3. Add the ADO.NET Entity Data Model from the existing database connection.
    4. Add Code Generation Item EF 5.x DbContext Generator, replacing the .tt files.
    5. 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?

  • rgettman
    rgettman over 10 years
    Please explain how this stops the exception.
  • simbolo
    simbolo over 10 years
    Changing the version to '6.7.4.0' worked in the machine.config. This must be an installer bug, as 6.7.4 was installed on a fresh installation of Windows, but the reference to 6.5.5 was still written to the machine.config.
  • ngm
    ngm over 10 years
    @rgettman It removes the existing configuration for MySQL Data Provider before adding the new one, so there is no complaint regarding duplicate entries.
  • Panagiotis Kanavos
    Panagiotis Kanavos over 10 years
    In my case I had to replace <remove name="MySQL Data Provider" /> with <remove invariant="MySql.Data.MySqlClient"/>