title: Integration testing multiple Entity framework dbcontexts that share a database
body: <p>In my application I have multiple small entity framework dbcontexts which share the same database, for example:</p>
  
  <pre><code>public class Context1 : DbContext {
      public Context1()
          : base("DemoDb") {
      }
  }
  
  public class Context2 : DbContext {
      public Context2()
          : base("DemoDb") {
      }
  }
  </code></pre>
  
  <p>All database updates are done via scripts and do not rely on migrations (nor will they going forward). The question is - how would you do integration testing against these contexts?</p>
  
  <p>I believe there are three options here (there may be more I just don't know them)</p>
  
  <p><strong>Option 1</strong> - Super context - a context which contains all models and configurations required for setting up the database:</p>
  
  <pre><code>public class SuperContext : DbContext
  {
      public SuperContext()
          : base("DemoDb") {
      }
  }
  </code></pre>
  
  <p>In this option the test database would be setup against the super context and all subsequent testing would be done through the smaller contexts. 
  The reason I am not keen on this option is that I will be duplicating all the configurations and entity models that i have already built.</p>
  
  <p><strong>Option 2</strong> - create a custom initialiser for integration tests that will run all the appropriate db initialisation scripts:</p>
  
  <pre><code>public class IntegrationTestInitializer : IDatabaseInitializer&lt;DbContext&gt; {
  
      public void InitializeDatabase(DbContext context) {
          /* run scripts to set up database here */
      }
  }
  </code></pre>
  
  <p>This option allows for testing against the true database structure but will also require updating everytime new db scripts are added</p>
  
  <p><strong>Option 3</strong> - just test the individual contexts:</p>
  
  <p>In this option one would just let EF create the test database based upon the context and all tests would operate within there own "sandbox". 
  The reason that I don't like this is that it doesn't feel like you would be testing against a true representation of the database.</p>
  
  <p>I'm currently swaying towards options 2. What do you all think? Is there a better method out there?</p>