Deleted database & migration file, can't make new migrations nor revert old migrations, what now? entity-framework-core entity-framework-core-3.0


My workflow with my ASP.NET Core app: Whenever I make changes that affect the database, I revert the old migration, make a new migration, apply it. Because this is still early development I think it makes sense to not pollute the project with lots of migration files. Usually I call this temporary migration file Initial, hoping that one of them will actually become the initial migration file.

As a result, I typically have 3 files in my Data/Migrations subfolder:

  • 00000000000000_CreateIdentitySchema.cs that was provided for me a long time ago by the project template;
  • somenumbers_Initial.cs;
  • ApplicationDbContextModelSnapshot.cs

So the commands I would type in this workflow would look like this, if I remember correctly:

Update-Database CreateIdentitySchema
Add-Migration Initial

This time I made a mistake... I don't remember what exactly happened, I typed something incorrect instead of the above commands and then I was silly enough that (a) manually removing the Initial migration file and (b) Dropping the database would be enough to restart the test db from scratch.

I was wrong.

My hopes were that Add-Migration Initial would create a valid migration file corresponding to the current state of the models and a subsequent Update-Database would recreate the db. Instead this is what happens:

PM> Add-Migration initial
      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.

??? Really? Well let's try to remove this migration:

      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.

???? I must manually remove the migration class ?? Where is this migration class?

Let's do some text searching in the project dir... The only interesting file that shows up is myprojectname.csproj where I have stuff like this:

    <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" />

I don't think this is relevant here (the numbers strings do not match).

I looked into ApplicationDbCOntextModelSnapshot but there seems to be no such class called initial there...

What to do now? How to proceed?

11/16/2019 8:31:46 PM

Accepted Answer

If you dropped the database then it's easy. Delete the migrations folder.

Then run

  • add-migration Initial
  • update-database

Also, I tend to remove any <Compile Remove="" /> from the project file.

11/17/2019 1:39:14 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow