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

12,094

Solution 1

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

Solution 2

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.

Solution 3

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

Reference : Entity Framework Scaffold-DbContext Login failed for user

Share:
12,094
Francesco B.
Author by

Francesco B.

Clean code enthusiast, wannabe metaprogrammer. https://stackoverflow.blog/2018/04/26/stack-overflow-isnt-very-welcoming-its-time-for-that-to-change/?cb=1

Updated on June 05, 2022

Comments

  • Francesco B.
    Francesco B. almost 2 years

    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'.