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

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

问题描述

我在.net core 1.1中使用单独的用户帐户标识创建新项目.我在Startup.cs中添加MySql提供程序:

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

但是当我尝试执行update-database时,会出现如下错误:

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标识或ASP.NET Core无关.通常,这与实体框架有关.更新数据库时,EF使用__EFMigrationsHistory记录执行了哪些迁移,因此以后不再执行它们.

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

解决方案很简单-自己创建表:

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

更新

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

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

鉴于MySQL的所有者是 Oracle ,不要期望连接器会取得很大的进步.还是数据库.

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?

解决方案

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.

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

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