Entity Framework Core Mastery

Reverse Engineering Existing Databases (Scaffolding)

2 Views Updated 5/6/2026

Reverse Engineering Existing Databases (Scaffolding)

In massive enterprise environments, you don't always have the luxury of starting a greenfield project where you dictate the schema via Code-First. You will often be asked to build a new API on top of a 15-year-old SQL Server database with 300 tables. Scaffolding allows EF Core to inspect the live database and automatically generate the C# Entity classes and the DbContext.

1. Installing the Required Tools

Scaffolding is a Design-Time operation. It does not happen when your web server is running. You must instruct the command-line tools to generate physical .cs files onto your hard drive.

# The CLI tool that executes EF Commands
dotnet tool install --global dotnet-ef

# The Design package required for scaffolding
dotnet add package Microsoft.EntityFrameworkCore.Design

# The Provider for your specific database (e.g., SQL Server)
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

2. Running the Scaffold Command

Using the CLI, we provide a connection string, designate the provider, and specify an output folder for the generated classes.

dotnet ef dbcontext scaffold "Server=...;Database=LegacyDb;User Id=...;Password=...;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -c LegacyDbContext

Breakdown:
-o Models: Puts all the generated C# classes in the 'Models' directory.
-c LegacyDbContext: Explicitly names the generated DbContext class.

3. Advanced Scaffolding Techniques

If the legacy database has 300 tables, but your API only needs to interact with the 'Users' and 'Invoices' tables, do NOT scaffold the entire database. Doing so generates thousands of lines of useless C# code that bloats your RAM.

Targeting Specific Tables

# Append the -t flag for every table you want to include
dotnet ef dbcontext scaffold "..." Microsoft.EntityFrameworkCore.SqlServer -o Models -t Users -t Invoices

Updating an Existing Scaffold (The Force Flag)

If the DBA adds a new column to the 'Invoices' table next week, you must re-run the scaffold. By default, EF Core will refuse to run because the Invoice.cs file already exists. You must append the force flag.

dotnet ef dbcontext scaffold "..." Microsoft.EntityFrameworkCore.SqlServer -o Models -t Invoices --force

4. Interview Mastery

Q: "If we use Database-First Scaffolding, and I manually edit the generated `Invoice.cs` file to add an `[EmailAddress]` validation attribute, what happens when the DBA alters the database and I run the scaffold command with the `--force` flag?"

Architect Answer: "The `--force` flag physically deletes and rewrites the entire `Invoice.cs` file on your hard drive. Your manual `[EmailAddress]` attribute will be completely erased. This is the primary danger of Database-First. To solve this, EF Core generates all entity classes as `partial` classes. You must create a SEPARATE physical file named `Invoice.Custom.cs` implementing the other half of the `partial class Invoice`. You can place your custom attributes, computed properties, or manual logic inside the custom file. Because the custom file is logically disconnected from the scaffolded file, running the `--force` flag will overwrite the generic scaffolding properties while your custom logic in the partial class remains perfectly safe."

Entity Framework Core Mastery
1. Foundations & Architecture
Introduction to Object Relational Mapping (ORM) Entity Framework Core Architecture & Providers Setup and DbContext Integration Code-First vs Database-First Approaches Reverse Engineering Existing Databases (Scaffolding)
2. Code-First Modeling
Entity Conventions & Data Annotations The Fluent API Deep Dive (OnModelCreating) Primary Keys, Composite Keys, & Guids Required Properties & Database Defaults Value Conversions (Enums & Strongly Typed IDs)
3. Relational Architecture
One-to-Many Relationships & Foreign Keys One-to-One Relationships (Dependent Entities) Many-to-Many Relationships & Navigation Properties Owned Entity Types (Value Objects) Table-per-Hierarchy (TPH) Inheritance
4. Data Querying & LINQ
Basic LINQ Queries & IQueryable Execution Tracking vs No-Tracking Queries (Performance) Eager Loading vs Explicit Loading (Include) Lazy Loading Pitfalls & Proxies Client vs Server Evaluation Parsing
5. Manipulating Data (CUD)
Adding, Updating, and Removing Entities The ChangeTracker and Entity States Disconnected Entities in Web APIs Batch Updates and Deletes (.NET 7+)
6. Advanced Performance & Scale
Concurrency Tokens and Optimistic Locking Raw SQL Queries and Views (FromSqlRaw) Compiled Queries for High Throughput Interceptors (Logging & Auditing Data Changes) DbContext Pooling Mechanisms Managing Complex EF Core Migrations (CI/CD)