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">