title: How to set up LocalDb for unit tests in Visual Studio 2012 and Entity Framework 5
body: <p>We have a Visual Studio 2012 ASP.NET MVC project using Entity Framework 5.</p>
  
  <p>There are some unit tests that depend on a database. Setting up the app.config file in the test project to use a central SQL Server database works fine.</p>
  
  <p>However, it would be much nicer to use a LocalDb, so that each developer has his/her own database when running the tests. Especially since we would like to have the tests set up to <code>DropCreateDatabaseAlways</code> when running.</p>
  
  <p>However, I can't get the setup to work. If I try this in app.config:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;add name="TestDb" 
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=unittestdb;
       Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\unittestdb.mdf"
     providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>I get: </p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: A file activation error occurred.
    The physical file name '\unittestdb.mdf' may be incorrect. Diagnose
    and correct additional errors, and retry the operation. CREATE
    DATABASE failed. Some file names listed could not be created. Check
    related errors.</p>
  </blockquote>
  
  <p>It sounds like it wants the mdf file to exist already, which seems strange since it is trying to create the database. Creating a mdf file manually does not change the error message.</p>