100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C#

Code-First vs Database-First

Comparing the Code-First and Database-First workflows in EF Core and how to choose between them, including hybrid approaches.

FoundationsIntermediate8 min readJul 10, 2026
Analogies

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.

bash
# 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 LegacyStoreContext

Re-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

Was this page helpful?

Topics covered

#EntityFrameworkCoreStudyNotes#MicrosoftTechnologies#CodeFirstVsDatabaseFirst#Code#Database#Workflow#Choosing#SQL#StudyNotes#SkillVeris