LocalDb login fails RRS feed

  • Question

  • There's not an appropriate sub-forum under .NET so posting here.  If there is a better place please advise how to get to it.

    LocalDB with EntityFramework 6 migrations.  ASP.NET MVC5 code first project.  VS 2013

    I'm working through Tom Dykstra Getting Started with EF6 using MVC5

    I'm logged in as 'MyUser' who has sysadmin rights to the SQL Server 2012 EXPRESS installation.

    When I run either 'databaseInitializer' or a migrations 'update-database' it fails with this error:

    Cannot open database "ContosoUniversity2" requested by the login. The login failed.
    Login failed for user 'MyMachine\MyUser'.

    From the web.config

    <add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

    <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity">
    <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity" />

    If I run VS 2013 as admin it successfully creates the DBs in the root of the Admin's User profile. If I then copy those DBs to the MyUser's user profile root and run VS 2013 not as admin the 'databaseInitializer' or a migrations 'update-database' work on the now pre-existing DBs.

    If I replace Initial Catalog with AttachDbFilename it creates the DB's in the apps App_Data folder proving that MyUser has create DB rights.  MyUser obviously has full rights to its own user profile folder.

    See full write up at

    I've seen other posts on the web about this problem with LocalDb.  Can anyone at the Microsoft Team look into this error?  Anyone else have any insight as to why LocalDb can't create databases for anyone but admin.


    Thursday, February 6, 2014 7:28 PM


All replies

  • Hello,

    >> Anyone else have any insight as to why LocalDb can't create databases for anyone but admin.

    Have a try to write the connection string like below with login information:

    <add name="TestString" connectionString="Data Source=(localdb)\v11.0;Database=Test;User ID=UserName;Password=Password;" providerName="System.Data.SqlClient"/>

    I made a test with this connection and it would create a database which I could do migrations operations on it successfully.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred BaoModerator Monday, February 17, 2014 5:20 AM
    • Unmarked as answer by 1278ts Friday, February 21, 2014 2:56 AM
    Monday, February 10, 2014 6:19 AM
  • Solution to Login fail error.

    If you delete the DB file, it still stays registered with SqlLocalDB. If LocalDb gets hung up with can’t attach (after manually deleting the DB file) or Login failed see JSobell’s and CodingwithSpike’s answers here:


    Run ‘sqllocaldb.exe stop v11.0’ and ‘sqllocaldb.exe delete v11.0’ from the PM Console


    • Marked as answer by 1278ts Friday, February 21, 2014 2:57 AM
    Friday, February 21, 2014 2:57 AM
  • Fred,

    Your post is a work around, not an answer to the question.  I've posted the answer to the OP.


    • Edited by 1278ts Friday, February 21, 2014 2:59 AM
    Friday, February 21, 2014 2:58 AM