title: Value cannot be null: connection while testing database
body: <p>I'm trying to run integration tests for my ASP.NET MVC application using Entity Framework 6.</p>
  
  <p>The error I get is </p>
  
  <blockquote>
    <p>System.Data.Entity.Core.EntityException: The underlying provider failed on Rollback. ---> System.ArgumentNullException: Value cannot be null.<br>
    Parameter name: connection</p>
  </blockquote>
  
  <p>The code looks like this:</p>
  
  <pre><code>Database.SetInitializer(new PrimaryInitializerTest());
  _context = new PrimaryContextTest();
  _context.Database.Initialize(true);
  
  using (var dbt = _context.Database.BeginTransaction())
  {
       dbt.Commit();
       dbt.Rollback();
  }
  </code></pre>
  
  <p>I also tried having an <code>dbt.UnderlyingTransaction.Connection.Open()</code> call just below the using statement, and a <code>dbt.UnderlyingTransaction.Connection.Close()</code> call just below the call to <code>Rollback()</code>. That gave me the error <code>Connection is not closed</code>.</p>
  
  <p><code>PrimaryInitializerTest</code> class</p>
  
  <pre><code>protected override void Seed(PrimaryContextTest context)
  {
      // (...) Input some values
      base.Seed(context);
  }
  </code></pre>
  
  <p><code>PrimaryContextTest</code> class</p>
  
  <pre><code>public class PrimaryContextTest : DbContext
  {
      public PrimaryContextTest() : base("PrimaryContextTest")
      {
          Database.SetInitializer(new DropCreateDatabaseAlways&lt;PrimaryContextTest&gt;());
      }
  
      public DbSet&lt;Story&gt; Stories { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          modelBuilder.Conventions.Remove&lt;IncludeMetadataConvention&gt;();
      }
  }
  </code></pre>
  
  <p>Connection string</p>
  
  <pre><code>&lt;add name="PrimaryContextTest" 
       connectionString="Data Source=(LocalDb)\mssqllocaldb;Initial Catalog=PrimaryContextTest;Integrated Security=SSPI;AttachDbFilename=|DataDirectory|\PrimaryContextTest.mdf" 
       providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>Context string</p>
  
  <pre><code>&lt;context type="fcon.DAL.Tests.PrimaryContextTest, fcon, Version=1.0.0.0, Culture=neutral"&gt;
      &lt;databaseInitializer type="fcon.DAL.Tests.PrimaryInitializerTest, fcon" /&gt;
  &lt;/context&gt;
  </code></pre>
  
  <p>What could I be doing wrong?</p>
  
  <p>Might mention that the database doesn't exist in the <code>App_Data</code> folder...</p>