MySQL connection in ASP.NET MVC 4 and Entity Framework 5.0 - multiple DbProviderFactories

10,405

I wrote a blog post on this subject, my project is available for download at the bottom of this paper.

I had to install (as noted in my article) MySqlDataEntity -Pre, which install the beta version 6.7.2 beta, to get good compatibility with EF 5, but I see that now the current version is noted 6.7.4, so I updated my project accordingly, and it works just as well, so I also updated the project to download from my blog.

Hope this can help you to start with EF and MySql.

BTW, in case this is not clear, you have to install, from NuGet, MySql.Data.Entities, which will also install MYSql.Data, as Entities is depending on it.

Share:
10,405

Related videos on Youtube

Boris
Author by

Boris

Software developer at D4tec

Updated on June 04, 2022

Comments

  • Boris
    Boris almost 2 years

    I have an ASP.NET application that I would like to upload to a host that offers a MySQL database. I am using Visual Studio 2012, .NET 4.5, Entity Framework 5.0, and I have installed MySQL for Visual Studio 1.0.2. I have been trying to connect to a local MySQL server, but I have encountered some problems.

    At first I referenced MySql.Data and MySql.Data.Entity at version 6.6.5.0 in my project. But when I tried to do Update-Database in the package manager console, I got the following error:

    System.DateTime System.Data.Entity.Migrations.Model.InsertHistoryOperation.get_CreatedOn()
    

    This seems to be a problem with Entity Framework 5.0 and earlier versions of Connector/.Net - the MySQL connector for visual studio. Since the process was one of discovery, I unfortunately can't recall how my project came to reference MySql.Data and MySql.Data.Entity version 6.6.5.0.

    I deleted the references, and then downloaded and referenced the same assemblies in version 6.7.4.0, using NuGet. This solved the error I got before, but now Update-Database gives me the following error:

    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)
    

    I have solved this problem by including a specific DbProviderFactory in my Web.Config file:

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

    If I leave out the remove tag, I get the error:

    Column 'InvariantName' is constrained to be unique.  Value 'MySql.Data.MySqlClient' is already present.
    

    I think this means that somehow my project still has a DbProviderFactory that serves version 6.6.5.0 of the MySQL libraries. But when I search the text files of my project, I can't find the string 6.6.5.0 anywhere.

    At the moment this solution lets me connect to my local MySQL server, but when I publish the project to my hosting provider and go to the web site in my browser, I just get an error message. I would like to know how to remove the implicit 6.6.5.0-serving DbProviderFactory, both as part of my learning and because I think my workaround could be the reason that my web site doesn't work at my hosting provider.


    As a clue in finding a solution, I find it suspicious that when I try to add a reference, I can only see (several versions of) 6.6.5.0:

    reference manager

    But when I go to the NuGet manager, I only have version 6.7.4.0 installed:

    nuget manager

    Hope this helps.