question

RichardLoba-6868 avatar image
1 Vote"
RichardLoba-6868 asked ErlandSommarskog answered

Tutorial: Get started with Razor Pages in ASP.NET Core Local DB login failure

https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-5.0&tabs=visual-studio

Thought I would try this quick tutorial.
I built the project and tried it out
The web app came up fine
When I tried to hit the database it failed, using the following connection string, created by the application.

 "RazorPagesMovieContext": "Server=(localdb)\\mssqllocaldb;Database=RazorPagesMovieContext-cdc6b0d0-adde-4a63-a778-1ea057d82af4;Trusted_Connection=True;MultipleActiveResultSets=true"

SqlException: Cannot open database "RazorPagesMovieContext-cdc6b0d0-adde-4a63-a778-1ea057d82af4" requested by the login. The login failed.
Login failed for user 'MrServer\MrMe'.
Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, bool breakConnection, Action<Action> wrapCloseInAction)


I even tried connecting using sa account (which is active), and running under administrator account.


I can login to SQLExpress using sa or MrServer\MrMe just fine through SSMS.


C:\Windows\System32\aspnetcoreapp>sqllocaldb info mssqllocaldb
Name: mssqllocaldb
Version: 14.0.2037.2
Shared name:
Owner: MrServer\MrMe
Auto-create: Yes
State: Running
Last start time: 4/8/2021 11:19:27 AM
Instance pipe name: np:\.\pipe\LOCALDB#80FA37E1\tsql\query

I have verified named pipes for SQL are setup
I'm using
Microsoft Visual Studio Community 2019
Version 16.9.3.
with .NET SDK 5.x
Windows 10

I have also tried it using Visual Studio Enterprise and it still fails.

The author was asked about this and the answer was, "it worked when he did it".
I'm thinking something typical may have changed with all of the updates to VS, SQL, Windows 10 causing this issue.

Any ideas on where to look next?

Thank you


sql-server-generaldotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My best guess is SQL Login was setup during SQL installation rather than mixed. The connection string you shared is using integrated security (Windows Authentication). So either configure the SQL instance to use Windows Authentication and add your Windows login account or change the connection to SQL login.

 "RazorPagesMovieContext": "Data Source=(localdb)\\MSSQLLocalDB;Database=RazorPagesMovieContext;User ID=YourUserName;Password=*******"


Regardless of the login, you'll need to make sure the account has proper read/write access in the SQL instance.

0 Votes 0 ·
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

Weird that it would default to the user\<username> directory

I looked on my machine, and it appears that the databases for the MSSQLLOCALDB is indeed also created directly in C:\Users\Username. It's not strange that they are in C:\users\, because a localdb instance runs your own user, but one would have expected the databases to be created on a nested folder somewhere.

Thanks for taking the time to share your findings!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichardLoba-6868 avatar image
0 Votes"
RichardLoba-6868 answered

Here to close this issue.
I tried the same thing with a new MVC core application, with the same results.
I tracked it down to the initial database creation. It tries to create it in the c:\users\<username> directory, and gets an access denied error.
Once I changed the security it worked fine.
Weird that it would default to the user\<username> directory.
Well, I got what I needed and when I get some time, I'll look into it.
Thank you all for your input and have great day!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered RichardLoba-6868 commented

It appears the application first tries to connect to SQL and fails with the user password.

No, it fails because the database specified in the connection string does not exist. Maybe there is a step in the exercise you have missed or skipped accidently?

You can connect to your localdb instance in SSMS by specifying (localdb)\MSSQLLOCALDB.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

yogyogi avatar image
0 Votes"
yogyogi answered yogyogi commented

Try connecting to the database using View > SQL Server Object Explorer in your visual studio. If you are able to connect it then it means your database name is correct else recheck the name of the database.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'll answer the suggestions above here, thanks for the responses.

Yes, I have tried the various suggestions, trusted connection= false, etc.

I'm unable to see the database in SSMS because it has not been created yet.

It appears the application first tries to connect to SQL and fails with the user password.

I'm assuming if it could connect to SQL server, it would then build the database based on the application's model (code first database creation.)

As far as the database being seen in SSMS;the local DB creation is done in the following folder.

C:\Users\MrMe\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb

This directory has a number of SQL databases, ones like you would see in SSMS...Only they are located here. The log files created by each run all have the same error, password not valid, etc

There are the files in that directory

mastlog.idf
modellog.idf
model.mdf
msdblog.ldf
tempdb.mdf
templog.idf
master.mdf

thank you


0 Votes 0 ·

It seems the database needs to be created first. We normally create the database using EF Core migrations most of the time. Once the database is created then seeding will work correctly. Try creating the database first through migration and then recheck the connection string and then run your app which will seed the database.


0 Votes 0 ·
JerryCai-MSFT avatar image
0 Votes"
JerryCai-MSFT answered

Hi,RichardLoba

First check whether the SQL Server has this database,(Click View->Sql Server Object Explorer->SqlServer->Database->your database name). mind whether the

long spell is right.

Whether you are connecting with SQL Server using Windows Authentication or SQL Authentication? If you are using Windows Authentication, try to add

Integrated Security = True; //if using SQL Authentication, set to False

Or try to check this link:

Error: Cannot open database requested by the login. The login failed. Login failed for user

Best Regards,
Jerry Cai


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichardLoba-6868 avatar image
0 Votes"
RichardLoba-6868 answered AgaveJoe commented

I'll answer both questions above.

Yes, SQL is setup for mixed mode, and sa is enabled. Ex: I can login to SQL using sa and password or integrated.

The 2nd question is one I have been trying to figure out as well.
I went looking for the local DB so I could just attach it and open it in SSMS, but I was unable to find it.
Since it was a login failure error I was thinking if it could not connect to SQL then it would not be able to load the database.
It may be the DB is created after the first successful login, and when it fails, SQL is done with me. :)

I have been able to find the local DBs created by MVC sites I have built in the past, but this one seems to be different.

Thank you for your responses, I'll keep digging, and maybe someone might have some ideas.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can find the database instance installed on your system by looking at opening the SQL Server Configuration Manager. Right click start -> Computer Management. Expand "Services and Applicaitons". Expand "SQL Server Configuration Manager". Click on SQL Server Services.

Make sure the instance(s) running on your system match the connection string created by the Visual Studio template.

Other way to get the connection string is making a connection using Visual Studio's "SQL Server Object Explorer". Once a connection is established, you can copy the connection string from the properties window.

0 Votes 0 ·
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

So is there actually a database by the name RazorPagesMovieContext-cdc6b0d0-adde-4a63-a778-1ea057d82af4 on your localdb instance?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.