Code-First vs Database-First
EF Core supports two primary workflows: Code-First, where you define C# entity classes and let EF Core generate and evolve the database schema through migrations, and Database-First, where an existing database schema is scaffolded into C# entity classes using the CLI.
Cricket analogy: Like building a team either by drafting promising academy players and developing them (Code-First) or picking up an already-established franchise squad via auction (Database-First), EF Core lets you either define classes and generate the schema or scaffold classes from an existing database.
The Code-First Workflow
In the Code-First workflow, you write POCO entity classes and a DbContext, then run dotnet ef migrations add <Name> to generate a versioned migration file and dotnet ef database update to apply it, giving greenfield projects a reviewable, incremental history of schema changes.
Cricket analogy: Like drafting a fresh set of playing conditions for a brand-new tournament and updating the rulebook version by version as changes are agreed, dotnet ef migrations add captures each schema change and dotnet ef database update applies it incrementally.
The Database-First Workflow
In the Database-First workflow, you run dotnet ef dbcontext scaffold against an existing database connection string, and EF Core generates matching entity classes and a DbContext by reading the schema — useful when the database already exists, is owned by another team, or predates your application (legacy systems).
Cricket analogy: Like a new coach inheriting an established team and building a scouting report from existing player data rather than picking players from scratch, dotnet ef dbcontext scaffold generates C# entity classes from an existing database's schema.
Choosing Between Them and Hybrid Approaches
Choosing between the two often comes down to who owns the schema: Code-First suits greenfield projects where the application team controls the database, while Database-First suits legacy or externally-owned databases. Hybrid approaches exist — scaffold once, then continue with Code-First migrations — but re-running scaffold overwrites any manual edits to the generated classes.
Cricket analogy: Like deciding whether to build a youth academy pipeline or acquire an established squad depending on whether you control player development, teams choose Code-First when they own the schema and Database-First when a DBA team owns an existing legacy database.
# Code-First: generate and apply migrations
dotnet ef migrations add InitialCreate
dotnet ef database update
# Database-First: scaffold entities from an existing database
dotnet ef dbcontext scaffold "Server=.;Database=LegacyStore;Trusted_Connection=True;" \
Microsoft.EntityFrameworkCore.SqlServer -o Models --context LegacyStoreContextRe-running dotnet ef dbcontext scaffold regenerates entity classes and overwrites any manual edits you made to them. If you need custom logic, use partial classes or separate configuration files instead of editing the scaffolded files directly.
- Code-First starts from C# entity classes; EF Core generates and evolves the schema via migrations.
- Database-First starts from an existing database; dotnet ef dbcontext scaffold generates matching C# classes.
- dotnet ef migrations add creates a versioned migration; dotnet ef database update applies it.
- Code-First suits greenfield projects where the application team owns the schema.
- Database-First suits legacy databases or databases owned by another team.
- Re-scaffolding overwrites hand-edited entity classes, so hybrid workflows need care (e.g., partial classes).
Practice what you learned
1. In the Code-First workflow, what generates the database schema?
2. Which command creates a new versioned migration file from your current model?
3. What does dotnet ef dbcontext scaffold do?
4. Database-First is typically preferred when:
5. What happens if you re-run dbcontext scaffold after manually editing the generated entity classes?
Was this page helpful?
You May Also Like
What Is Entity Framework Core?
An introduction to Entity Framework Core, Microsoft's cross-platform ORM for .NET, and why teams use it instead of raw ADO.NET.
DbContext and DbSet
How DbContext and DbSet work together as EF Core's session and table-collection abstractions, including change tracking and fluent configuration.
Your First Query
Writing and executing your first LINQ query against EF Core, including deferred execution, projections, async queries, and the N+1 pitfall.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics