Note: this article is published on 01/18/2025
After I wrote several articles on this site, I found out it seemed almost for every article, I needed to set up a sample application associated with an entity framework if accessing the database. And, every time, I needed to rewrite the setup process from scratch in order for a new reader to follow along easily. Even for introducing a very simple concept, such as Caching, I needed to spend 80% of the time setting up the sample app, and only 20% on introducing the Caching concept itself.
Therefore, I think it is better to write a basic model such as entity framework sample for various approaches, and then I can reuse them when needed. I made a list of the series of articles below, I will write them one by one, while the Entity framework overview and concept will be covered in the article (0):
- Entity Framework (0), Overview
- Entity Framework (1), with .Net MVC, Code-First
- Entity Framework (2), with .Net MVC, Database-First
- Entity Framework (3), with .Net MVC, Model-First
- Entity Framework (4), with .Net Core MVC, Code-First
- Entity Framework (5), with .Net Core MVC, Database-First
- Entity Framework (6), with .Net Core MVC, Model-First
- Entity Framework (7), with .Net WPF, Database-First
- Entity Framework (8), with .NET Core Web API, Stored Procedure
- Entity Framework (9), with .NET Core Web API, Stored Procedure Implementation
- Entity Framework (10), with .Net WebForms, Database-First
- Entity Framework (11), with .Net Core Razor Pages Code-First
- Entity Framework (12), with New .Net Core MVC Code-First
- Entity Framework (13), with .Net Core Code-First Summary
Introduction
This article is a detailed discussion of the Reverse Engineering command used in the article Entity Framework (5), with .Net Core MVC, Database-First: Scaffold-DbContext.
There are two parts for this article:
- A - AI Result for
Scaffold-DbContext
- B - My sample from work
At the end, you have an MVC app that can consume a database directly through entity framework.
A - AI Result for Scaffold-DbContext
AI Result from Copilot | Microsoft 365 Copilot
The Scaffold-DbContext
command is used in Entity Framework Core to generate entity type classes and a DbContext
class based on an existing database schema. This process is known as reverse engineering. Here’s a quick guide on how to use it:
-
Install the necessary tools:
-
Run the Scaffold-DbContext command:
-
Parameters:
YourConnectionString
: The connection string to your database.
Microsoft.EntityFrameworkCore.SqlServer
: The provider for your database (e.g., SQL Server).
-
Example:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=YourDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer
This command will generate the entity classes and the DbContext
class in your project, allowing you to interact with your existing database using EF Core [1], [2], [3]
B - My sample from work
Scaffold-DbContext -Connection name=CoreDb -Provider Microsoft.EntityFrameworkCore.SqlServer -Project "Willdan.Viewpoint.LADWP.DataAccess" -OutputDir CoreModels -ContextDir Context -Context CoreContext -Tables Application, FieldOption, FieldReference, NaicsCode, NotificationHistory, NotificationHistoryAttachment, NotificationHistoryRecipient, Organization, OrganizationLaborRate, OrganizationMaterialSupply, Organization_Program, Role, User, UserMetaData, User_Program, User_Role
Command Reference --- EF Core tools reference - Scaffold-DbContext
Reference