EF Core `update-database` on MySql fails with `__EFMigrationsHistory' doesn't exist`

asp.net-core c# entity-framework entity-framework-core mysql

Question

I Create new project in .net core 1.1 with individual user account identity. I Add MySql Provider in Startup.cs:

services.AddDbContext<ApplicationDbContext>(options => 
        options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));

But when i try do update-database i get error like this:

MySql.Data.MySqlClient.MySqlException: Table 'cloud.__EFMigrationsHistory' doesn't exist    at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
    at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.EntityFrameworkCore.Storage.Internal.MySQLRelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
    at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations() 
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType) 
   at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
 Table 'cloud.__EFMigrationsHistory' doesn't exist 

What I should do?

1
2
9/7/2017 7:14:23 AM

Accepted Answer

This isn't related to ASP.NET Identity or ASP.NET Core. This is related to Entity Framework in general. When you update a database, EF uses the __EFMigrationsHistory to record which migrations were executed so it doesn't perform them again in the future.

This functionality is implemented by the database provider, not EF itself. There was at least one case where the Npgsql provider for PostgresSQL didn't create the table.

The solution is easy - create the table yourself :

CREATE TABLE `__EFMigrationsHistory` 
( 
    `MigrationId` nvarchar(150) NOT NULL, 
    `ProductVersion` nvarchar(32) NOT NULL, 
     PRIMARY KEY (`MigrationId`) 
);

UPDATE

There was another similar question in 2016. This is a bug of the official MySQL provider. The fix is to create the table. Not the only one either. Asynchronous operations are faked by running them on a different thread for example.

I'd suggest you investigate third-party MySQL providers like Pomelo.EntityFrameworkCore.MySql. They found and fixed the migration history bug 1 year ago.

Given that the owner of MySQL is Oracle, don't expect a lot of progress on the connector. Or the database.

6
5/4/2018 7:12:35 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow