Entity Framework Core Migrations

Database migrations let you evolve your database schema over time while preserving existing data. This guide walks you through creating, applying, and managing migrations in an ASP.NET Core project.

1️⃣ Add the EF Core Tools

dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet tool install --global dotnet-ef

2️⃣ Create Your First Migration

After defining your DbContext and entity classes, run:

dotnet ef migrations add InitialCreate

3️⃣ Apply the Migration

Use dotnet ef database update to apply pending migrations to the database.

dotnet ef database update

4️⃣ Common Scenarios

5️⃣ Managing Migration History

EF Core stores migration history in the __EFMigrationsHistory table. You can view it with:

dotnet ef migrations list

💡 Tips & Best Practices

📚 Further Reading

Explore more advanced topics: