Entity Framework Core Interview Question #100
What are best practices for EF Core migrations and schema management?
Migrations, Database Schema & Data Management Mid-Level Intermediate
Quick Interview Answer
Keep migration files and the model snapshot in source control, give migrations meaningful names, and review generated operations before committing or deploying them. Never assume generated schema changes are automatically safe.
Detailed Explanation
Keep migration files and the model snapshot in source control, give migrations meaningful names, and review generated operations before committing or deploying them. Never assume generated schema changes are automatically safe.
Use controlled production deployment steps, verify the target database and connection, and consider generating reviewed SQL scripts where operational governance requires it. Avoid rewriting migration history after it has been shared or deployed.
Design schema evolution for compatibility: use appropriate types, lengths, keys, indexes, constraints, and delete behavior; plan destructive changes in stages; test migrations against realistic data; and maintain reliable backups and recovery procedures.
Code Example
# Typical development workflow
dotnet ef migrations add AddProductSearchIndex
dotnet ef migrations script --idempotent -o migration.sql
# Review generated migration and SQL before
# applying changes to important environments.