今天在迁移的数据库脚本并生成 sql 文件时出现了下面的错误,两个迁移的cs文件名称我使用了migration1 和 migration2 替代;
The migration '20240911024302_migration2' was not found.
命令:
Script-Migration -From migration1 -To migration2
出现如下错误提示:
Build started...
Build succeeded.
System.InvalidOperationException: The migration 'migration2' was not found.
at Microsoft.EntityFrameworkCore.Migrations.MigrationsAssemblyExtensions.GetMigrationId(IMigrationsAssembly assembly, String nameOrId)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.PopulateMigrations(IEnumerable`1 appliedMigrationEntries, String targetMigration, IReadOnlyList`1& migrationsToApply, IReadOnlyList`1& migrationsToRevert, Migration& actualTargetMigration)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateScript(String fromMigration, String toMigration, MigrationsSqlGenerationOptions options)
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrator.GenerateScript(String fromMigration, String toMigration, MigrationsSqlGenerationOptions options)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.ScriptMigration(String fromMigration, String toMigration, MigrationsSqlGenerationOptions options, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigrationImpl(String fromMigration, String toMigration, Boolean idempotent, Boolean noTransactions, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The migration 'migration2' was not found.
当然在这里 我很确定 'migration2' 迁移文件是存在的,当我再尝试运行 Add-Migration 时,migration2 又再次生成了,没有提示已经存在该迁移文件,事实上该文件是存在的,且 出现两个,就前面的时间字符串不同。对于这个问题到现在我没找到原因
后我改用cli 的命令来执行生成和迁移 又是正常的,所以知道的大佬告知下原因,感谢!
dotnet ef migrations script migrations1 migrations2 --output .\myscript.sql
这样去生成脚本却又正常,没有提示 'migration2' 文件不存在