Scaffold-DbContext "Login Failed" "Error Number:4060,State:1,Class:11"


My ASP.NET MVC core application should connect to an existing MSSQL LocalDB file via Entity Framework.

Database-first development requires reverse-engineering the existing database. Following the instructions in the official documentation, I am running the following command in the NuGet Package Manager Console:

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

However I am getting

No design-time services were found.
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "DatabaseName" requested by the login. The login failed.
Login failed for user 'MACHINE-NAME\UserName'.
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)

Error Number:4060,State:1,Class:11
Cannot open database "DatabaseName" requested by the login. The login failed.
Login failed for user 'MACHINE-NAME\UserName'.
asked on Stack Overflow Feb 11, 2018 by Francesco B.

3 Answers


Since "Login failed" might indicate that the database cannot be located, I decided to copy-paste the Connection String in Visual Studio Server Explorer, found by right-clicking the database and picking "Properties".

So the new command was

Scaffold-DbContext "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\...AbsolutePath...\DatabaseFileName.mdf;Integrated Security=True;Connect Timeout=30" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -verbose

and... it worked. So:

  1. Instead of Server, I used Data Source
  2. Instead of using the database name, I wrote the full path to the LocalDb file
answered on Stack Overflow Feb 11, 2018 by Francesco B.

What worked for me was to specify the command as follows:

Scaffold-DbContext "Data Source={MY-COMPUTER-NAME};Initial Catalog={DATABASE-NAME};Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Replace {MY-COMPUTER-NAME} and {DATABASE-NAME} with values found in Studio's Server Explorer. I am running SQL Server (not Express) on my computer.

answered on Stack Overflow Jun 26, 2018 by Kluge

As per other thread, you password may contains special char like $,

Reference : Entity Framework Scaffold-DbContext Login failed for user

answered on Stack Overflow Sep 19, 2019 by Consisty

User contributions licensed under CC BY-SA 3.0