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.
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
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.
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.
# Append the -t flag for every table you want to include
dotnet ef dbcontext scaffold "..." Microsoft.EntityFrameworkCore.SqlServer -o Models -t Users -t Invoices
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
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."