title: EntityFramework Core automatic migration
body: <p>In my project I have 2 databases: one is my custom database and the other one is <code>ApplicationDbContext</code> that <code>Microsoft Identity</code> gives me.</p>
  <p>In my <code>Startup.cs</code> I have this code:</p>
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
          ApplicationDbContext db, MyContext dbPSC)
  {
       // ...
  
      db.Database.EnsureCreated();
      db.Database.Migrate();
  
      dbPSC.Database.EnsureCreated();
      dbPSC.Database.Migrate();
  }
  </code></pre>
  <p>I don't know when there is a migration to migrate, right? Then, I have 2 errors:</p>
  <ul>
  <li>the migration for <code>ApplicationDbContext</code> raises an error every time the application starts apart from the first time</li>
  <li>the migration for my context seems fine</li>
  </ul>
  <p>What is the best practise to use? Is it necessary to call the migration for <code>ApplicationDbContext</code>?</p>
  <h2>Update</h2>
  <p>I have removed the <code>Migration</code> folder. Then, changed the <code>Startup.cs</code> like</p>
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
          ApplicationDbContext db, MyContext dbPSC)
  {
       // ...
  
      db.Database.EnsureCreated();
      dbPSC.Database.EnsureCreated();
  }
  </code></pre>
  <p>but when the application starts, it doesn't create any tables at all. <code>AuditDbContext</code> is because I use <a href="https://github.com/thepirat000/Audit.NET/" rel="nofollow noreferrer">Audit.net</a></p>
  <pre><code>public class MyContext : AuditDbContext
  {
      public MyContext(DbContextOptions&lt;MyContext&gt; options) : base(options) { }
  
      public DbSet&lt;Message&gt; Messages { get; set; }
      public DbSet&lt;AuditMessage&gt; Audit_Messages { get; set; }
  
      #region Common Tables
      public DbSet&lt;Country&gt; Countries { get; set; }
      public DbSet&lt;AuditCountry&gt; Audit_Countries { get; set; }
      #endregion
      #region Seed
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Seed();
      }
      #endregion
  }
  </code></pre>
  <h2>Update #2</h2>
  <p>I tried another solution but it doesn't work. The solution is to use <code>RelationalDatabaseCreator</code> like the following code</p>
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
          ApplicationDbContext db, MyContext dbPSC)
  {
       // ...
  
      db.Database.EnsureCreated();
      dbPSC.Database.EnsureCreated();
      RelationalDatabaseCreator databaseCreator = 
       (RelationalDatabaseCreator)context.Database.GetService&lt;IDatabaseCreator&gt;();
      databaseCreator.CreateTables();
  }
  </code></pre>
  <p>As the <code>Migrate()</code>, the first time the app runs it creates the tables but the second time it raises an error because the tables are already exist in the database.</p>