PierreDuv
PierreDuv

Reputation: 124

How to enable migration for EF 6.4 and MySQL C#?

I tried to enable migrations using the command

Enable-Migrations

But I got the error below, I didn't really understand how to solve it...

I installed all the extensions to make it works. enter image description here

The error message :

Checking if the context targets an existing database...
No MigrationSqlGenerator found for provider 'MySql.Data.MySqlClient'. Use the SetSqlGenerator method in the target migrations configuration class to register additional SQL generators.

Upvotes: 1

Views: 7429

Answers (2)

jrivam
jrivam

Reputation: 1101

as the error message says, you need to change first the MigrationSqlGenerator. https://learn.microsoft.com/en-us/ef/ef6/fundamentals/configuring/code-based

you can do it in 2 ways:

modify the entityFramework section in your App.Config

<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly">
    ...Your EF config...
</entityFramework>

OR add the DbConfigurationType attribute to your DbContext inherited class

[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssembly")]
public class MyContextContext : DbContext
{
}

for MySql.Data.Entity -> MySql.Data (>= 6.10.9)

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <providers>
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
    ...other providers
    </providers>
  </entityFramework>

//

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6))]
public class DemoContext : DbContext
{
}

for MySql.Data.EntityFramework -> MySql.Data (>= 8.0.20)

<entityFramework codeConfigurationType="MySql.Data.EntityFramework.MySqlEFConfiguration, MySql.Data.EntityFramework">
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.20.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    ...other providers
    </providers>
  </entityFramework>

//

[DbConfigurationType(typeof(MySql.Data.EntityFramework.MySqlEFConfiguration, MySql.Data.EntityFramework))]
public class DemoContext : DbContext
{
}

Upvotes: 4

Samuel
Samuel

Reputation: 368

I assume your project is a .net core application and you are using visual studio.

  1. Make sure you have Microsoft.Entityframeworkcore.tools install from Nuget Package Manager.
  2. From your package manager console, you don't need to enable migration as done previously in ef 5
  3. Just run Add-Migration {MigrationName}
  4. Run Update-Database to update the database

OR

  1. if the AppDbContext is in the same project with the startup file
  2. Run dotnet ef migrations add {MigrationName}
  3. Run dotnet ef -database update to update the database

OR

  1. if the AppDbContext is in the different project, Open the containing project root directory from the command line
  2. Run dotnet ef migrations add {MigrationName} --s ../{startupproject}/{startupproject.csproj}
  3. Run dotnet ef -database update to update the database --s ../{startupproject}/{startupproject.csproj}

Upvotes: 0

Related Questions