Tutorials Entity Framework Core Mastery

Reverse Engineering Existing Databases (Scaffolding)

On this page

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."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Mastery
Course syllabus
1. Foundations & Architecture
2. Code-First Modeling
3. Relational Architecture
4. Data Querying & LINQ
5. Manipulating Data (CUD)
6. Advanced Performance & Scale
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details