title: EF6 migrations (LocalDb) update-database login failed
body: <p>VS 2013 MVC5 code first project.</p>
  
  <p>I’m working through the ASP.NET <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc" rel="noreferrer">Getting Started with EF6 using MVC5</a> tutorial to get up to speed on the latest changes and I’m having a problem with migrations.
  In the first module the database is created using a database initializer:</p>
  
  <pre><code>&lt;contexts&gt;
        &lt;context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity"&gt;
          &lt;databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" /&gt;
        &lt;/context&gt;
  &lt;/contexts&gt;
  </code></pre>
  
  <p>and this connection string:</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>This works fine.  In the Code First Migrations and Deployment module migrations are setup.  The initializer is commented out and the name of the DB is changed to ContosoUniversity2:</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>When <code>update-database</code> is then run from the Packager Manager Console it fails with the error message:</p>
  
  <pre><code>Cannot open database "ContosoUniversity2" requested by the login. The login failed.
  Login failed for user 'MyMachine\MyUser'.
  </code></pre>
  
  <p>I’ve re-run both scenarios several times with the same user and the same results.  If I change <code>Initial Catalog</code> to <code>AttachDbFilename=|DataDirectory|\ContosoUniversity2;</code> <code>update-database</code> succeeds (the DB is now in the App-Data folder of the project rather the root of the users profile):</p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0; AttachDbFilename=|DataDirectory|\ContosoUniversity2;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>You have to use <code>Initial Catalog</code> when deploying though.  The production connectionString is set separately in the Web.Release.Config so that is workable.</p>
  
  <p>The question is why the need for the fix, why doesn’t <code>Initial Catalog</code> work with <code>update-database</code> on the development side?</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>The problem is not with migrations but with <a href="http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx" rel="noreferrer">LocalDb</a></p>
  
  <p><code>MyUser</code> has full rights in <code>SQLExpress</code> (<code>sysadmin</code>).  I can log into <code>SSMS</code> under <code>MyUser</code> and fully manage <code>DBs</code>.  I ran several tests.  Even though I created the initial DB for the tutorial as <code>MyUser</code> it now throws the login failed error if I rebuild the app from scratch and use <code>databaseInitialzer</code>.  If I run <code>VS</code> under <code>Admin</code> both <code>databaseInitialzer</code> and <code>update-database</code> work without any problem.  If I then copy the <code>DBs</code> from the <code>Admin's</code> user profile root to <code>MyUser's</code> and then run <code>VS</code>, not as <code>Admin</code>, while logged on as <code>MyUser</code> both the <code>databaseInitialzer</code> and <code>update-database</code> then work if the <code>DB</code> is prior existing.</p>
  
  <p>If I change the <code>connectionString</code> to <code>AttachDbFileName</code> and run either <code>databaseInitialzer</code> and <code>update-database</code> <code>MyUser</code> can create a <code>DB</code> in the <code>App_Data</code> folder proving <code>MyUser</code> has <code>DB</code> create rights.  <code>MyUser</code> obviously has full rights to the root of its own user profile.  There is something amisss with the <a href="http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx" rel="noreferrer">LocalDb</a> implementation.</p>
  
  <p>Does any one have any insight on this question?</p>