Code-First vs Database-First in EF Core (ASP.NET Core)
๐ก Concept Name
Code-First vs Database-First in EF Core
๐ Quick Intro
EF Core supports two main workflows: Code-First and Database-First. Code-First creates the DB from code, while Database-First creates code from an existing DB.
๐ง Analogy / Short Story
Think of building a house. With Code-First, you're the architect โ you design the blueprint, and the house (database) is built accordingly. With Database-First, you walk into an already-built house, take measurements, and then sketch the blueprint (models) from it.
๐ง Technical Explanation
In Code-First, you define your entity models in C# and use migrations to create or update the database schema. In Database-First, you use `Scaffold-DbContext` to generate models and DbContext from an existing database schema.
๐ฏ Purpose & Use Case
- โ Use Code-First when starting fresh and want full control over schema
- โ Use Database-First when working with a legacy or pre-existing DB
- โ Code-First fits agile workflows, version control for schema
- โ Database-First suits database-first enterprise environments
- โ Code-First supports advanced features like Fluent API configurations
๐ป Real Code Example
// Code-First: Apply migrations
dotnet ef migrations add InitialCreate
dotnet ef database update
// Database-First: Scaffold from existing DB
dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer

โ Interview Q&A
Q1: What's the key difference between Code-First and DB-First?
A: Code-First generates DB from code; DB-First generates code from DB.
Q2: Which EF Core command is used for scaffolding?
A: `Scaffold-DbContext` or `dotnet ef dbcontext scaffold`.
Q3: What are EF Migrations used for?
A: Managing schema changes over time in Code-First.
Q4: Is Code-First better than DB-First?
A: It depends on project type; Code-First is better for new projects.
Q5: Can EF Core reverse engineer an existing database?
A: Yes, with scaffolding.
Q6: Can I modify models after scaffolding?
A: Yes, but be cautious; resync may overwrite changes.
Q7: How do I control table names in Code-First?
A: Use Data Annotations or Fluent API.
Q8: Which approach is easier to version control schema with?
A: Code-First, due to migration scripts.
Q9: Do both approaches use DbContext?
A: Yes, it's central in both.
Q10: Can I switch from DB-First to Code-First later?
A: Yes, but requires careful restructuring.
Q1: Which EF Core command scaffolds a DbContext from an existing database?
- A. dotnet ef update
- B. dotnet ef create
- C. dotnet ef dbcontext scaffold
- D. dotnet ef generate
Q2: Code-First approach uses which of the following to update DB schema?
- A. Migrations
- B. DB Sync
- C. Seeders
- D. Manual SQL
๐ก Bonus Insight
Hybrid approach is also possible โ start with DB-First, then customize code and switch to Code-First with migrations. EF Core is flexible enough to evolve with your project needs.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!