Cannot open database "Fix4Missing" requested by the login. The login failed.
(error 4060) means the server accepted who you are but the database named in
Database= is not there, or your login has no access to it. Create the database
or correct the name, and the connection opens.
Check the spelling against the server's list of databases first. If the name is right, the
database was never created: in an EF Core project that usually means the migrations were
never applied, so run dotnet ef database update or call
Database.Migrate() at startup.
The error
PS> .\FixLab.exe 'Server=(localdb)\MSSQLLocalDB;Database=Fix4Missing;Integrated Security=true;TrustServerCertificate=true' 'SELECT DB_NAME()'
ex.Number = 4060
Microsoft.Data.SqlClient.SqlException: Cannot open database "Fix4Missing" requested by the login. The login failed.
Login failed for user 'MACHINE\user'.
The Windows account name in the last line is replaced with MACHINE\user;
nothing else changed.
Why it happens
Opening a connection is two steps: the server authenticates the login, then switches to the
database in Database= (also called Initial Catalog). Here the
first step succeeded, which is why the message names the Windows user, and the second
failed because no database called Fix4Missing exists on the LocalDB instance.
SQL Server reports both cases, "does not exist" and "exists but you may not use it", with
the same text, so the message alone does not tell you which one you have.
The common ways to get here: a typo or a different name between environments, a fresh machine where nobody has created the database yet, an EF Core app whose migrations have not run, or a SQL login on a shared server that has no user in that database.
The fix
PS> sqlcmd -S '(localdb)\MSSQLLocalDB' -E -Q 'CREATE DATABASE Fix4Missing'
PS> .\FixLab.exe 'Server=(localdb)\MSSQLLocalDB;Database=Fix4Missing;Integrated Security=true;TrustServerCertificate=true' 'SELECT DB_NAME()'
Fix4Missing
OK (-1 row(s) affected)
After the database existed, the unchanged connection string opened and
DB_NAME() returned it. To see which databases do exist, connect with
Database=master and run SELECT name FROM sys.databases. In EF Core,
Database.Migrate() creates the database and applies the migrations;
EnsureCreated() creates it from the model without migrations, which suits tests
and prototypes but cannot be migrated later. If the database exists and only your login is
refused, an administrator has to create a user for that login in the database.
How it was reproduced
A console app from dotnet new console -n FixLab -f net10.0 (.NET SDK 10.0.401)
with Microsoft.Data.SqlClient 7.1.1, which opens a connection from its first argument and
prints ex.Number and the message on failure. It connected with Windows
authentication to SQL Server 2022 LocalDB 16.0.1200.5 on Windows 11, asking for
Fix4Missing, a database name chosen for the test that did not exist. The
database was then created with sqlcmd 16 and the same command connected.
Frequently asked
- What does SQL Server error 4060 Cannot open database requested by the login mean?
- The login itself was accepted, but the database named in the connection string either does not exist on that server or the login has no user in it. Check the name, create the database, or grant access.
- Why does my EF Core app say Cannot open database on first run?
- The database has not been created yet. Run dotnet ef database update, or call Database.Migrate() at startup so EF Core creates the database and applies the migrations.
- How do I list the databases on LocalDB?
- Connect to (localdb)\MSSQLLocalDB with Database=master and run SELECT name FROM sys.databases, or use sqlcmd -S (localdb)\MSSQLLocalDB -E with the same query.
More decoded errors in the Fixes category.