The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file

3

I have a project in MVC, and wanted to connect to the MySql database. I put this connectionStrings .

<add name="Contexto" connectionString="server=127.0.0.1;User Id=xxxxxx;password=xxxxx;database=iesb_site" providerName="MySql.Data.MySqlClient" />

but every time I make Update-Migrations the following error appears:

  The ADO.NET provider with invariant name 'MySql.Data.MySqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.

    
asked by anonymous 21.02.2017 / 15:31

1 answer

9

To make the Entity Framework work with MySQL is not as simple as with SQL Server.

Before you need to make some settings.

  • Install the MySql.Data.Entity.EF6 package

      

    Install-Package MySql.Data.Entity.EF6

  • Edit the <entityFramework> section in the web.config file (or app.config ) and add the provider MySql.Data 4 of the example - replacing the entire section with this one should also work)

       
    <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
      <providers>
        <provider invariantName="MySql.Data.MySqlClient" 
                  type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
        <provider invariantName="System.Data.SqlClient" 
                  type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
      </providers>
    </entityFramework>
    
  • Set a new DbConfiguration class for MySQL. This can be done in three ways:

    3.1. Add the DbConfigurationType attribute in the current context class

    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class MeuContexto: DbContext { }
    

    3.2. Call DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) whenever application is initialized. For example, in an ASP.NET MVC application, this code can be placed in the Application_Start method of the Global.asax file.

    3.3. Referencing the class MySqlEFConfiguration in the web.config file

    <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    
  • 21.02.2017 / 17:24