| name | create-migration |
| description | Create and manage Entity Framework Core database migrations including schema changes, seed data, and rollback strategies. Use when adding or modifying EF Core entities, setting up a new DbContext, applying data seeding, running dotnet ef commands, or troubleshooting migration conflicts. |
Create EF Migration
Create, review, and apply Entity Framework Core migrations safely.
Workflow
- Make entity/model changes in code
- Generate migration with a descriptive name
- Review the generated migration — verify no unintended column drops or data loss
- Apply to local database and verify
- Commit migration files with the related entity changes
Creating Migrations
dotnet ef migrations add AddInvoiceLineItems --project src/MyApp.Data --startup-project src/MyApp.Api
dotnet ef database update --project src/MyApp.Data --startup-project src/MyApp.Api
dotnet ef database update PreviousMigrationName --project src/MyApp.Data --startup-project src/MyApp.Api
dotnet ef migrations remove --project src/MyApp.Data --startup-project src/MyApp.Api
Best Practices
- Descriptive names:
AddInvoiceLineItems, RenameUserEmailToContactEmail — never Update1
- Small and focused: One schema change per migration for easier rollback
- Seed data: Use
HasData() in OnModelCreating or IEntityTypeConfiguration<T>, not raw SQL in migrations
- Review before applying: Check the generated
Up() and Down() methods for unintended drops or data loss
- Test locally: Always apply against a local database before committing
Checklist