title: How to use new database instance of each MSTest unit test run
body: <p>I'm using MSTest under Visual Studio 2010 to test an ASP.NET MVC 3 project. I have a SQL Express 2005 DB that I'd like it to use and I want a fresh instance of the DB every time, copied from a template I've included in the project. Pretty standard requirements, I would have though, but I can't get this to work.</p>
  
  <p>I've created a .testsettings file which enables deployment and my connection string looks like this:</p>
  
  <pre><code>&lt;add name="MyDb" connectionString="Data Source=.\SQLEXPRESS2005;Database=MyDbTest;AttachDBFilename=|DataDirectory|MyDbTest.mdf;User Instance=true;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>The test runs OK the first time, but after that it fails with errors like this:</p>
  
  <pre><code>Test method ... threw exception:  System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details. ---&gt; System.Data.EntityException: The underlying provider failed on Open.
  ---&gt; System.Data.SqlClient.SqlException: Database '...\bin\Debug\MyDbTest.mdf' already exists. Choose a different database name. Cannot attach the file '...\Out\MyDbTest.mdf' as database 'MyDbTest'.
  </code></pre>
  
  <p>The accepted answer in <a href="http://social.msdn.microsoft.com/Forums/en-AU/sqlexpress/thread/b8e46209-1fd1-4c66-b886-ae25233a17f3" rel="nofollow">this MSDN thead</a> says to remove the "Database=" connection string parameter. However, if I do that it fails with this error:</p>
  
  <pre><code>Test method ... threw exception: 
  System.InvalidOperationException: Unable to complete operation. The supplied SqlConnection does not specify an initial catalog.
  </code></pre>
  
  <p>How do I get this to work?</p>