Entity Framework (5-1), With .Net Core MVC, Database-First --- Scaffold-DbContext

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):

Introduction

This article is a detailed discussion of the Reverse Engineering command used in the article Entity Framework (5), with .Net Core MVC, Database-FirstScaffold-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:

  1. Install the necessary tools:

    • For Package Manager Console (PMC) in Visual Studio:
      Install-Package Microsoft.EntityFrameworkCore.Tools
      
    • For .NET CLI:
      dotnet tool install --global dotnet-ef
      
  2. Run the Scaffold-DbContext command:

    • In PMC:
      Scaffold-DbContext "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer
      
    • In .NET CLI:
      dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer
      
  3. Parameters:

    • YourConnectionString: The connection string to your database.
    • Microsoft.EntityFrameworkCore.SqlServer: The provider for your database (e.g., SQL Server).
  4. 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 [[[]

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