How to get MySQL working with Microsoft .NET’s Entity Framework 6

  1. Download and install the latest MySQL .NET Connector.
  2. Add [MySQL.Data] and [MySql.Data.Entity for EF6] assembly references for your .NET Framework version (hover over the assembly to see its framework version in its .dll path).
  3. Use NuGet Package Manager to install Entity Framework.
  4. Modify Web.config’s <entityFramework> section to MySQL values (this will avoid the “No Entity Framework provider found for the ADO.NET provider with invariant name ‘MySql.Data.MySqlClient’.” error):
    <entityframework>
      <defaultconnectionfactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6">
        <parameters>
          <parameter value="v11.0"></parameter>
        </parameters>
      <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
      </providers>
    </defaultconnectionfactory></entityframework>
    
  5. Add the following to Web.config’s <configuration> section:
    <system .data>
      <dbproviderfactories>
        <remove invariant="MySql.Data.MySqlClient"></remove>
        <add name="MySQL Data Provider"
             invariant="MySql.Data.MySqlClient"
             description="Data Provider for MySQL"
             type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></add>
      </dbproviderfactories>
    </system>
    <connectionstrings>
      <add name="Storage"
           connectionString="server=localhost; User Id=user; Password=password; Persist Security Info=True; database=db_name;"
           providerName="MySql.Data.MySqlClient"></add>
    </connectionstrings>
    
  6. For migrations to work, add this to your migration Configuration class’ constructor:
    SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());

Leave a Reply

Your email address will not be published. Required fields are marked *

*