Tutorials ASP.NET Core Complete Tutorial (ShopNest)
Database First Approach with EF Core (Scaffold)
Learn Database First Approach with EF Core (Scaffold) in our free ASP.NET Core Complete Tutorial (ShopNest) series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.
On this page
Introduction
Not every ShopNest integration starts greenfield. Database First scaffolds entities from an existing HR database — common when migrating legacy systems at TCS/Wipro-style maintenance projects.
After this article you will
- Run dotnet ef dbcontext scaffold with options
- Extend scaffolded models with partial classes
- Refresh after DBA schema changes
- Map stored procedures and database views
- Mix Code First for new modules on same DB
Prerequisites
- Article 20 — EF Core Performance Optimization
- ShopNest DbContext with Products/Orders from Articles 13–15
- SQL Server or LocalDB configured
Concept deep-dive
dotnet ef dbcontext scaffold "Server=.;Database=LegacyHR;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models/Scaffolded --context-dir Data --context LegacyHrContext --force --no-onconfiguring
Partial classes: never edit scaffolded files — add Employee.Partial.cs with computed properties.
// Keyless entity for DB view
modelBuilder.Entity<EmployeeSummaryView>(b =>
{
b.HasNoKey();
b.ToView("vw_EmployeeSummary");
});
// Stored procedure
var results = await _db.EmployeeBonuses
.FromSqlRaw("EXEC hr.GetBonuses @Year = {0}", 2025)
.ToListAsync();
Hands-on — ShopNest Legacy HR System Migration
- Scaffold from sample LegacyHR database (or LocalDB script).
- Partial class adds FullName computed property.
- Map reporting view as keyless entity for read-only dashboard.
- Document re-scaffold workflow for team when DBA adds columns.
Common errors & best practices
- Editing generated files — overwritten on re-scaffold; use partials.
- Scaffolding entire server with 200 tables — scaffold specific tables with --table flags.
Interview questions
Q: When Database First?
A: Legacy DB, no ownership of schema, brownfield migration.
Q: Code First vs Database First?
A: Code First — dev owns schema. Database First — DBA owns schema, app adapts.
Summary
- Scaffold-DbContext reverse-engineers existing SQL Server
- Partial classes extend without losing regeneration
- Views and sprocs map to keyless entities / FromSqlRaw
- Common in legacy HR migration scenarios
Previous: EF Core Performance Optimization
Next: EF Core with SQL Server — Advanced Features
FAQ
Can I mix Code First migrations with scaffolded context?
Use separate contexts or carefully merge — one DbContext per bounded context is cleaner.
How update after schema change?
Re-scaffold with --force or add migration if you own schema via Code First.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!