title: EFCore: migrations and initial DB creation
body: <p>I'm using EFCore (recently migrated from EF) to do code-first DBs with coded migrations. EFCore updates the context model snapshot whenever I create a new migration. So when I create a new DB from scratch using</p>
  
  <pre><code>using (var db = new AudmDatabaseContext())
  {
      if (!db.Database.GetService&lt;Microsoft.EntityFrameworkCore.Storage.IRelationalDatabaseCreator&gt;().Exists())
      {
          db.Database.EnsureCreated();
      }
  }
  </code></pre>
  
  <p>I have the current DB structure. However, when I have an existing system, I need to run migrations</p>
  
  <pre><code>using (var db = new AudmDatabaseContext())
  {
      if (db.Database.GetService&lt;Microsoft.EntityFrameworkCore.Storage.IRelationalDatabaseCreator&gt;().Exists())
      {
              var pendingMigrations = db.Database.GetPendingMigrations();
              if (pendingMigrations.Count() &gt; 0)
              {
                  db.Database.Migrate();
               }
           }
       }
  }
  </code></pre>
  
  <p>Now, imagine I created a new DB using my DB creation code. Then at some later point, I'm changing the DB schema, so I need to run migrations. But, when I do that, I get all migrations as pending, and the migrator tries to make changes to the DB schema that have already been made - which obviously doesn't work. (note that my initial migration has all code commented out as otherwise it would create the entire DB again but that's already being done with db.Database.EnsureCreated())</p>
  
  <p>So what's the recommended approach here? I need to be able to create new DBs for new installations for my software, and I need to be able to migrate (so I actually set up my app to execute migrations automatically on startup to never miss any changes).</p>
  
  <p>The most obvious that comes to mind would be to have some kind of flag that tells the migrator to run the migrations but actually not execute any sql code when doing the initial create - so a newly created DB would already appear to be fully migrated to the migrator.. and when at some later point I have new pending migrations, only these new pending migrations that have been added to the code since I created the initial DB would be executed.</p>
  
  <p>I'm just curious what other people are doing since I seem to be unable to find the recommended approach to this problem in the official documentation.</p>