数据库和迁移文件已删除,既不能进行新迁移也不能还原旧迁移,现在该怎么办?

时间:2019-11-16 20:31:46

标签: asp.net-core entity-framework-core entity-framework-core-3.0

我使用ASP.NET Core应用程序的工作流程:每当进行影响数据库的更改时,我都会还原旧的迁移,进行新的迁移,然后应用它。因为这仍是早期开发,所以我认为不使用大量迁移文件来污染项目是有意义的。通常,我将此临时迁移文件称为Initial,希望其中一个可以真正成为初始迁移文件。

因此,我的Data/Migrations子文件夹中通常有3个文件:

  • 00000000000000_CreateIdentitySchema.cs是很久以前由项目模板提供给我的;
  • somenumbers_Initial.cs;
  • ApplicationDbContextModelSnapshot.cs

因此,如果我没记错的话,我将在此工作流程中键入的命令如下所示:

Update-Database CreateIdentitySchema
Remove-Migration
Add-Migration Initial
Update-Database

这次,我犯了一个错误...我不记得发生了什么,我输入了不正确的命令而不是上面的命令,然后我很傻,以至于(a)手动删除了Initial迁移文件(b)删除数据库就足以从头开始重新启动测试数据库。

我错了。

我希望Add-Migration Initial将创建一个与模型当前状态相对应的有效迁移文件,随后的Update-Database将重新创建数据库。相反,这是发生了什么:

PM> Add-Migration initial
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.0.0 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
The name 'initial' is used by an existing migration.

???真?好吧,让我们尝试删除此迁移:

Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.0.0 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
No file named '20190727172711_initial.cs' was found. You must manually remove the migration class 'initial'.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(String value)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty property, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(String builderName, IProperty property, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(String builderName, IEnumerable`1 properties, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList`1 entityTypes, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateSnapshot(String modelSnapshotNamespace, Type contextType, String modelSnapshotName, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration.<>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)
Object reference not set to an instance of an object.

????我必须手动删除迁移类?该迁移课程在哪里?

让我们在项目目录中进行一些文本搜索...出现的唯一有趣的文件是myprojectname.csproj,其中包含以下内容:

    <Compile Remove="Data\Migrations\20190412182930_initial.cs" />
    <Compile Remove="Data\Migrations\20190412182930_initial.Designer.cs" />
    <Compile Remove="Data\Migrations\20190628205310_initial.cs" />
    <Compile Remove="Data\Migrations\20190628205310_initial.Designer.cs" />
    <Compile Remove="Data\Migrations\20190720205411_initial.cs" />
    <Compile Remove="Data\Migrations\20190720205411_initial.Designer.cs" />
    <Compile Remove="Data\Migrations\20190720205837_initial.cs" />
    <Compile Remove="Data\Migrations\20190720205837_initial.Designer.cs" />

我认为这与这里无关(数字字符串不匹配)。

我调查了ApplicationDbCOntextModelSnapshot,但似乎那里没有这样的类叫做initial ...

现在该怎么办?如何进行?

1 个答案:

答案 0 :(得分:1)

如果删除了数据库,这很容易。删除迁移文件夹。

然后运行

  • $`0` IS_new pseq a 15 0.5098 Q 15 16 0.5055 G 16 17 0.5807 S 17 18 0.6427 L 18 19 0.7209 T 19 20 0.6906 S 20 21 0.7629 S 21 22 0.7629 T 22 23 0.8279 T 23 24 0.8493 T 24 25 0.8853 T 25 26 0.7982 S 26 27 0.7718 S 27 28 0.7415 P 28 29 0.7881 G 29 30 0.7120 A 30 31 0.7209 G 31 32 0.7209 S 32 33 0.6227 S 33 34 0.5533 S 34 35 0.5493 P 35 36 0.5620 W 36 37 0.5017 A 37 $`1` IS_new pseq a 38 0.5098 A 38 $`2` IS_new pseq a 41 0.5211 N 41 58 0.5665 A 58
  • add-migration Initial

此外,我倾向于从项目文件中删除任何update-database