title: EntityFramework in test initialization error: CREATE DATABASE statement not allowed within multi-statement transaction
body: <p>I'm trying to build a quick test that deletes and recreates a database every time it runs.  I have the following:</p>
  
  <pre><code>[TestClass]
  public class PocoTest
  {
      private TransactionScope _transactionScope;
      private ProjectDataSource _dataSource; 
      private Repository _repository = new Repository();
      private const string _cstring = "Data Source=.;Initial Catalog=test_db;Trusted_Connection=True";
  
      [TestInitialize]
      public virtual void TestInitialize()
      {
          _dataSource = new ProjectDataSource(_cstring);
          _dataSource.Database.Delete();
          _dataSource.Database.CreateIfNotExists();
          _transactionScope = new TransactionScope();
      }
      [TestMethod]
      public void TestBasicOperations()
      {                
          var item = _repository.AddItem(new Item(){Details = "Test Item"});
          //  AddItem makes a call through the data context to add a set and then calls datacontext.SaveChanges()
      }
  
  
      [TestCleanup]
      public void TestCleanup()
      {
          // rollback
          if (_transactionScope != null)
          {
              _transactionScope.Dispose();
          }
      }
  </code></pre>
  
  <p>However when I run the test I get the following error: </p>
  
  <blockquote>
    <p>Result Message:    Test method
    Project.Repository.UnitTests.PocoTest.TestBasicOperations threw
    exception:  System.Data.SqlClient.SqlException: CREATE DATABASE
    statement not allowed within multi-statement transaction.</p>
  </blockquote>
  
  <p>ProjectDataSource is here: </p>
  
  <pre><code>public class ProjectDataSource : DbContext, IProjectDataSource
  {
  
      public ProjectDataSource() : base("DefaultConnection")
      {
  
      }
  
      public ProjectDataSource(string connectionString) : base(connectionString)
      {
  
      }
  
      public DbSet&lt;Set&gt; Sets { get; set; }
  }
  </code></pre>
  
  <p>Repository: </p>
  
  <pre><code>public class Repository : IRepository
  {
      private readonly ProjectDataSource _db = new ProjectDataSource();
      public Item AddItem(Item item)
          {
              _db.Items.Add(item);
              _db.SaveChanges();
              return item;
          }
  }
  </code></pre>
  
  <p>Why is this happening?  </p>
  
  <p>Also - if it makes any difference - the error doesn't occur if I comment out the AddItem line in TestMethod.</p>