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

scaffold-dbcontext with username and password
scaffold-dbcontext views
scaffold-dbcontext not recognized
scaffold-dbcontext build failed
scaffold-dbcontext dataannotations
scaffold-dbcontext update model from database
scaffold-dbcontext mysql
scaffold-dbcontext force flag

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:

  1. Instead of Server, I used Data Source
  2. Instead of using the database name, I wrote the full path to the LocalDb file

Reverse Engineering, Understanding EFCore Scaffold-DbContext Commands in .NET Core to create the scaffolding for single or multiple tables for a given schema or  Scaffold-DbContext commands help scaffolding entity type classes and a DbContext class based on a database schema thereby automating the code generation technique related to database access. We shall try covering below aspects overall in this article, Install EFCore scaffolding Tools Configuring EFCore using Visual Studio or VSCode

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.

EF Core tools reference (.NET CLI), One can solve the problem by usage of dotnet ef dbcontext scaffold command with multiple -t ( --table ) parameters. It allows to specify all the  Updating the model Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema. It can be performed using the Scaffold-DbContext command of the EF Core Package Manager Console (PMC) tools or the dotnet ef dbcontext scaffold command of the.NET Command-line Interface (CLI) tools.

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

Reference : Entity Framework Scaffold-DbContext Login failed for user

Entity Framework Core with Existing Database, Scaffolding a database produces an Entity Framework model from an existing database. dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root  Scaffold-DbContext This is the command that you should use when you have an existing database and you want to generate from it a data context and all the respective classes that represent the tables of said database. We call this procedure database first. We have an entry which talks more about this technique.

Generating a model from an existing database, When using Scaffold-DbContext to reverse-engineer VIEW objects from SQL Server, any columns in the selected VIEW objects of type "int" are  Scaffolding DbContext and Models with EntityFramework Core 2.0 and the CLI 07 September 2017 Posted in.NET Core, ef core, command line, CLI EF Core 2.0 has been out for a few weeks now. If you're looking at an ORM for your.NET application then EF Core should be at the top of the list of possible options.

Understanding EFCore Scaffold-DbContext Commands , I am trying to generate a DbContext for an existing database structure using ASP.NET 5 and Entity Framework 7. Not surprisingly, there isn't a lot of documentation surrounding how to do this easily.

Can we Scaffold DbContext from selected tables of an existing , In order to scaffold a DbContext from an existing database, you first have to set up project.json file. You need to add reference of Entity Framework tools in the project.json file tools section. For this post I am generating DbContext and model classes from Sqlite Database. So I am using EF Sqlite references as well.

  • Thanks! Worked for me