ef core mysql 生成迁移失败,MySql上的EF Core`update-database`失败,`__EFMigrationsHistory'不存在。...

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

services.AddDbContext(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.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?

解决方案

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值