Migrations in EF Core (ASP.NET Core)
๐ก Concept Name
EF Core Migrations in ASP.NET Core
๐ Quick Intro
EF Core Migrations help keep your database schema in sync with your models. You define your C# models, and EF Core generates migration scripts to apply schema changes.
๐ง Analogy / Short Story
Think of your project as a growing building. Each migration is a construction order โ it adds a new floor, changes layout, or upgrades plumbing. EF Core Migrations act like your construction manager, tracking changes and updating the database as your models evolve.
๐ง Technical Explanation
Migrations are a Code-First feature of EF Core. They generate C# code that represents schema changes. You can add migrations with `dotnet ef migrations add`, update the database using `dotnet ef database update`, and even revert using `dotnet ef migrations remove`.
๐ฏ Purpose & Use Case
- โ Track and manage changes to your database schema
- โ Keep database in sync with your C# models
- โ Automate schema deployment across environments
- โ Generate version-controlled scripts
- โ Revert changes using rollback strategies
๐ป Real Code Example
// Add initial migration
dotnet ef migrations add InitialCreate
// Apply migration to the database
dotnet ef database update
// Remove last migration (before applying)
dotnet ef migrations remove

โ Interview Q&A
Q1: What is EF Core Migrations?
A: A way to incrementally update the database schema based on model changes.
Q2: Which command creates a migration?
A: `dotnet ef migrations add MigrationName`
Q3: How do you update the DB with pending migrations?
A: Use `dotnet ef database update`
Q4: Can you revert a migration?
A: Yes, using `dotnet ef migrations remove` (before it's applied)
Q5: Where are migrations stored?
A: In the `Migrations` folder as C# classes
Q6: What is `ModelSnapshot`?
A: A file that stores the current model state for comparison
Q7: Can you apply migrations at runtime?
A: Yes, with `context.Database.Migrate()` in code
Q8: What happens if a migration fails during deployment?
A: It throws an exception, halting the update
Q9: How do you seed data with migrations?
A: Use `HasData()` in `OnModelCreating()`
Q10: Are migrations mandatory in EF Core?
A: No, but highly recommended for Code-First projects
Q1: Which command adds a new migration in EF Core?
- A. dotnet ef migrations add
- B. dotnet ef create
- C. dotnet ef build
- D. dotnet add migration
Q2: Which command applies migrations to the DB?
- A. dotnet ef database update
- B. dotnet ef migrate
- C. dotnet ef publish
- D. dotnet ef sync
Q3: What does `dotnet ef migrations remove` do?
- A. Deletes entire schema
- B. Removes all migrations
- C. Removes the last migration (if not applied)
- D. Resets the DbContext
Q4: What is `ModelSnapshot` used for?
- A. Logging
- B. Backups
- C. Track current model structure
- D. Debugging only
Q5: Can you generate SQL script from migration?
- A. Yes, using `dotnet ef migrations script`
- B. No
- C. Only manually
- D. Using DB designer only
Q6: Which EF Core method runs all migrations programmatically?
- A. context.Database.Migrate()
- B. context.Sync()
- C. context.UpdateSchema()
- D. context.ApplyChanges()
Q7: Where is seed data defined in EF Core?
- A. OnModelCreating()
- B. Startup.cs
- C. DbInitializer
- D. appsettings.json
Q8: Which file contains migration history in SQL Server?
- A. __EFMigrationsHistory
- B. EF_Track
- C. Migration_Log
- D. Schema_EF
Q9: Which EF Core command generates a migration SQL script?
- A. dotnet ef migrations script
- B. dotnet ef sql generate
- C. dotnet ef create script
- D. dotnet ef dump
Q10: What is required to run EF Core commands?
- A. SDK installed only
- B. Microsoft.EntityFrameworkCore.Design package
- C. IDE only
- D. NuGet.exe
๐ก Bonus Insight
Migrations can be added to CI/CD pipelines to apply schema updates automatically. You can also generate raw SQL scripts from migrations for DBAs to review before execution.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!