记一次ef+mysql执行update出错的解决

  • 时间:2019年04月08日
简介 使用Entity Framework连接MySql执行Update命令报错:Table ''demoDb.__EFMigrationsHistory'' doesn''t exist

对于Entity Framework的使用一直停留在ef+mssql阶段,趁着有空,在docker上捣鼓了一个mysql;

然后创建模型类,配置连接字符串;

执行初始化;

dotnet ef migrations add InitialCreate

然后执行update;

dotnet ef database update

然后就粗事儿了;

 

Failed executing DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT `MigrationId`, `ProductVersion`
FROM `__EFMigrationsHistory`
ORDER BY `MigrationId`;
MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'demoDb.__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 Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   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.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Table 'demoDb.__EFMigrationsHistory' doesn't exist

为嘛呢?

因为EF在执行更新的时候会使用表 “__EFMigrationsHistory”来记录执行了哪些迁移,但是在mysql中没有这个表,所以就报错了;

解决方法呢,很简单啊,手动创建一个就好啦;

CREATE TABLE __EFMigrationsHistory

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

end; 

Top