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

asp.net-core asp.net-core-mvc entity-framework-core localdb sql-server

Question

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'.
1
3
2/11/2018 9:24:02 PM

Accepted Answer

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
4
2/11/2018 9:24:02 PM

Popular Answer

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

Reference : Entity Framework Scaffold-DbContext Login failed for user



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow