MySql 上的 EF Core `update-database` 失败,因为 `__EFMigrationsHistory' 不存在` [英] EF Core `update-database` on MySql fails with `__EFMigrationsHistory' doesn't exist`

查看:63
本文介绍了MySql 上的 EF Core `update-database` 失败,因为 `__EFMigrationsHistory' 不存在`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用个人用户帐户身份在 .net core 1.1 中创建新项目.我在 Startup.cs 中添加 MySql Provider:

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

但是当我尝试执行 update-database 时,我得到如下错误:

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 

我该怎么办?

推荐答案

这与 ASP.NET Identity 或 ASP.NET Core 无关.这通常与实体框架有关.当您更新数据库时,EF 使用 __EFMigrationsHistory 来记录执行了哪些迁移,以便将来不再执行这些迁移.

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.

此功能由数据库提供者实现,而不是 EF 本身.至少有一种情况是 PostgresSQL 的 Npgsql 提供程序没有创建表.

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`) 
);

更新

另一个类似问题2016. 这是官方 MySQL 提供程序的错误.解决方法是创建表.也不是唯一的.例如,通过在不同线程上运行异步操作来伪造异步操作.

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.

我建议您调查第三方 MySQL 提供商,例如 Pomelo.EntityFrameworkCore.MySql.他们发现并修复了 1 年前的迁移历史错误.

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

鉴于 MySQL 的所有者是 Oracle,不要指望在连接器上取得很大进展.或者数据库.

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

这篇关于MySql 上的 EF Core `update-database` 失败,因为 `__EFMigrationsHistory' 不存在`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆