title: Entity Framework Code First: How to seed a database for unit testing
body: <p>My question and code is based on the <a href="http://www.arrangeactassert.com/code-first-entity-framework-unit-test-examples/" rel="noreferrer">Code First Entity Framework Unit Test Examples</a> blog post. I am using SQL Compact 4.0 and as such my unit tests are running against the actual database using real data similar to what is being described in the blog post.</p>
  
  <p>I want to seed my production database with default values in some of the tables but when running my unit tests I want to add additional data and update some of the default values.</p>
  
  <p>I have created a custom Initializer class that seeds the database with the default values. For my unit tests I have created another custom Initializer that inherits from the first one that does the test specific seeding and/or modifications:</p>
  
  <pre><code>public class NerdDinnersInitializer : DropCreateDatabaseIfModelChanges&lt;NerdDinners&gt;
  {
      protected override void Seed(NerdDinners context)
      {
          var dinners = new List&lt;Dinner&gt;
                            {
                                new Dinner()
                                    {
                                        Title = "Dinner with the Queen",
                                        Address = "Buckingham Palace",
                                        EventDate = DateTime.Now,
                                        HostedBy = "Liz and Phil",
                                        Country = "England"
                                    }
                            };
  
          dinners.ForEach(d =&gt; context.Dinners.Add(d));
  
          context.SaveChanges();
      }
  }
  
  public class NerdDinnersInitializerForTesting : NerdDinnersInitializer
  {
      protected override void Seed(NerdDinners context)
      {
          base.Seed(context);
  
          var dinner = context.Dinners.Where(d =&gt; d.Country == "England").Single();
          dinner.Country = "Ireland";
  
          context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>I also use a base class for my unit tests that initializes the test database like so:</p>
  
  <pre><code>[TestClass]
  public abstract class TestBase
  {
      protected const string DbFile = "test.sdf";
      protected const string Password = "1234567890";
      protected NerdDinners DataContext;
  
      [TestInitialize]
      public void InitTest()
      {
          Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "",
                  string.Format("Data Source=\"{0}\";Password={1}", DbFile, Password));
          Database.SetInitializer(new NerdDinnersInitializerForTesting());
  
          DataContext = new NerdDinners();
          DataContext.Database.Initialize(true);
      }
  
      [TestCleanup]
      public void CleanupTest()
      {
          DataContext.Dispose();
  
          if (File.Exists(DbFile))
          {
              File.Delete(DbFile);
          }
      }
  }
  </code></pre>
  
  <p>The actual unit test looks like this:</p>
  
  <pre><code>[TestClass]
  public class UnitTest1 : TestBase
  {
      [TestMethod]
      public void TestMethod1()
      {
          var dinner = new Dinner()
                            {
                                Title = "Dinner with Sam",
                                Address = "Home",
                                EventDate = DateTime.Now,
                                HostedBy = "The wife",
                                Country = "Italy"
                            };
  
          DataContext.Dinners.Add(dinner);
          DataContext.SaveChanges();
  
          var savedDinner = (from d in DataContext.Dinners
                             where d.DinnerId == dinner.DinnerId
                             select d).Single();
  
          Assert.AreEqual(dinner.Address, savedDinner.Address);
      }
  }
  </code></pre>
  
  <p>When I run the test the Linq query that fetches the savedDinner fails with the "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection." exception. I cannot work out why.</p>
  
  <p>Is what I am doing here an acceptable pattern and can anyone shed some light as to why this is not working?</p>
  
  <p>Thanks.</p>