title: Entity Framework Migrations - How to create a Unit Test to Ensure Migrations Model is up to Date?
body: <p>I am using continuous integration with TeamCity, NUnit, and Git. I recently migrated (pardon the pun) from FluentMigrator to Entity Framework Migrations. I primarily did this to take advantage of the scaffolding functionality therein.</p>
  
  <p>However, it is potentially possible to check in some changes to source control without having first scaffolded the changes into migrations (imagine the scenario where the application was not run before committing and pushing the commit). I am using a <a href="http://www.joergm.com/2011/05/pre-tested-commits-using-git/" rel="noreferrer">pre-tested commit workflow</a>, so I would like to detect this problem in the pre-test rather than waiting until calling migrate.exe (which would be too late in my workflow and break the "green repository").</p>
  
  <p>My question is, how to create a unit/integration test to detect when the migrations model doesn't match the context model so I can fail the build before attempting to migrate? Do note that I expect it not to match the database and would prefer not to access the database from the test.</p>
  
  <p>I tried this approach:</p>
  
  <pre><code>    [Test]
      public void CheckWhetherEntityFrameworkMigrationsContextIsUpToDate()
      {
          Assert.DoesNotThrow(() =&gt;
          {
              CallDatabase();
          });
  
      }
  
      private void CallDatabase()
      {
          using (var ctx = new MyContext("SERVER=(local);DATABASE=MyDatabase;Integrated Security=True;"))
          {
              var tenant = (from t in ctx.Tenant
                            select t).FirstOrDefault();
          }
      }
  </code></pre>
  
  <p>But this will always fail when there are pending migrations (rather than just in the case where the migrations model is not in sync with the context model, which is what I am after).</p>
  
  <h2>Update</h2>
  
  <p>I have added a <a href="https://entityframework.codeplex.com/workitem/1727" rel="noreferrer">work item</a> on the EntityFramework project for this issue. Hopefully, they will look into adding a way to do this.</p>