title: MigrateDatabaseToLatestVersion not executed
body: <p>I can't figure out why my latest migration is not getting executed automatically on application startup (or at least on first access of the database context). I used to run <em>update-database</em> manually in development, but I want to test whether it will upgrade automatically on my hosted test environment.</p>
  
  <p><strong>In Application_Start():</strong></p>
  
  <pre><code>Database.SetInitializer&lt;FepazoContext&gt;(
      new MigrateDatabaseToLatestVersion&lt;FepazoContext, FepazoConfiguration&gt;())
  </code></pre>
  
  <p><strong>In FepazoConfiguration:</strong></p>
  
  <pre><code>internal sealed class FepazoConfiguration : 
      DbMigrationsConfiguration&lt;Fepazo.Models.FepazoContext&gt;
  {
      public FepazoConfiguration()
      {
          AutomaticMigrationsEnabled = true;
      }
  }
  </code></pre>
  
  <p><strong>I even added this to the constructor of FepazoContext:</strong></p>
  
  <pre><code>public FepazoContext() : base("DefaultConnection")
  {
      Database.Initialize(false);
  }
  </code></pre>
  
  <p>Some extra information:</p>
  
  <ul>
  <li>The migration was automatically created through <em>add-migration</em> and looks ok. </li>
  <li>When I query the <code>__MigrationHistory</code> table, I can see that the migration is not yet 'recorded' as executed.</li>
  <li>I verified that the initializer or the <code>AutomaticMigrationsEnabled</code> setting isn't overridden in Web.config file.</li>
  <li>Breakpoints in <code>FepazoContext</code> constructor and the <code>FepazoConfiguration</code> <em>are</em> getting hit.</li>
  </ul>
  
  <p>Am I forgetting something ? Can I dig deeper to find out where it goes wrong ?</p>
  
  <p><strong>Updates</strong></p>
  
  <p>Passing <code>True</code> to <code>Database.Initialize</code> to try and force the migration also has no effect. <code>Database.CompatibleWithModel(true)</code> returns false - so the system detects there is a difference, however it does not execute the pending migration!</p>
  
  <pre><code>public FepazoContext() : base("DefaultConnection")
  {
      if (!Database.CompatibleWithModel(true))
      {
          // This is executed (each time the code enters)
          Database.Initialize(true);
      }
  }
  </code></pre>
  
  <p><strong>Workaround</strong></p>
  
  <p>As a workaround, I call <code>DbMigrator.Update()</code> explicitly right after setting the initializer. That does the trick, although I'd still like to know why it doesn't work automatically...</p>
  
  <pre><code>protected void Application_Start()
  {
      // &lt;...&gt;
      Database.SetInitializer&lt;FepazoContext&gt;(
          new MigrateDatabaseToLatestVersion&lt;FepazoContext, FepazoConfiguration&gt;());
      var dbMigrator = new DbMigrator(new FepazoConfiguration());
      dbMigrator.Update();
      // &lt;...&gt;
  }
  </code></pre>