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
00000000000000_CreateIdentitySchema.csthat was provided for me a long time ago by the project template;
So the commands I would type in this workflow would look like this, if I remember correctly:
Update-Database CreateIdentitySchema Remove-Migration Add-Migration Initial Update-Database
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 Microsoft.EntityFrameworkCore.Infrastructure 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:
Microsoft.EntityFrameworkCore.Infrastructure 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
What to do now? How to proceed?
If you dropped the database then it's easy. Delete the migrations folder.
Also, I tend to remove any
<Compile Remove="" /> from the project file.