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'.
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:
Server
, I used Data Source
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.
As per other thread, you password may contains special char like $,
Reference : Entity Framework Scaffold-DbContext Login failed for user
User contributions licensed under CC BY-SA 3.0