title: Deleted database & migration file, can't make new migrations nor revert old migrations, what now?
body: <p>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 <code>Initial</code>, hoping that one of them will actually become the initial migration file.</p>
  
  <p>As a result, I typically have 3 files in my <code>Data/Migrations</code> subfolder:</p>
  
  <ul>
  <li><code>00000000000000_CreateIdentitySchema.cs</code> that was provided for me a long time ago by the project template;</li>
  <li><code>somenumbers_Initial.cs</code>;</li>
  <li><code>ApplicationDbContextModelSnapshot.cs</code></li>
  </ul>
  
  <p>So the commands I would type in this workflow would look like this, if I remember correctly:</p>
  
  <pre><code>Update-Database CreateIdentitySchema
  Remove-Migration
  Add-Migration Initial
  Update-Database
  </code></pre>
  
  <p>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 <code>Initial</code> migration file and (b) Dropping the database would be enough to restart the test db from scratch.</p>
  
  <p>I was wrong.</p>
  
  <p>My hopes were that <code>Add-Migration Initial</code> would create a valid migration file corresponding to the current state of the models and a subsequent <code>Update-Database</code> would recreate the db. Instead this is what happens:</p>
  
  <pre><code>PM&gt; 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.
  </code></pre>
  
  <p>??? Really? Well let's try to remove this migration:</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClass0_0.&lt;.ctor&gt;b__0()
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.&lt;&gt;c__DisplayClass3_0`1.&lt;Execute&gt;b__0()
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  Object reference not set to an instance of an object.
  </code></pre>
  
  <p>???? I must manually remove the migration class ?? Where is this migration class?</p>
  
  <p>Let's do some text searching in the project dir... The only interesting file that shows up is <code>myprojectname.csproj</code> where I have stuff like this:</p>
  
  <pre><code>    &lt;Compile Remove="Data\Migrations\20190412182930_initial.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190412182930_initial.Designer.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190628205310_initial.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190628205310_initial.Designer.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190720205411_initial.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190720205411_initial.Designer.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190720205837_initial.cs" /&gt;
      &lt;Compile Remove="Data\Migrations\20190720205837_initial.Designer.cs" /&gt;
  </code></pre>
  
  <p>I don't think this is relevant here (the numbers strings do not match).</p>
  
  <p>I looked into <code>ApplicationDbCOntextModelSnapshot</code> but there seems to be no such class called <code>initial</code> there...</p>
  
  <p>What to do now? How to proceed?</p>