The Problem
Model has fields → Database missing those fields → App crashes
Detection (30 seconds)
# Check actual database
psql -h localhost -U postgres -d YOUR_DB -c "\d YOUR_TABLE"
# Check EF thinks database has
dotnet ef dbcontext script --context YourContext | grep -A 20 "CREATE TABLE.*YourTable"
Fix Pattern (5 steps)
1. Fix the Snapshot
Edit DBContextModelSnapshot.cs
→ Remove fields that don’t exist in real database
2. Create Migration
dotnet ef migrations add "AddMissingFields" --project YOUR_PROJECT --context YourContext
3. Review Migration
Check the generated .cs
file → Ensure it adds the missing fields
4. Apply Migration
dotnet ef database update --project YOUR_PROJECT --context YourContext
5. Verify
psql -h localhost -U postgres -d YOUR_DB -c "\d YOUR_TABLE"
Prevention Rules
- Never manually edit database – Always use migrations
- Model change = Migration immediately – Don’t wait
- Always apply migrations after creating them
- Check pending migrations before deployment:
dotnet ef migrations list --context YourContext
Production Deploy Checklist
- [ ] Run migrations on staging first
- [ ] Backup production database
- [ ] Check no pending migrations:
dotnet ef migrations list
- [ ] Deploy + run migrations
- [ ] Verify schema matches model
Golden Rule: If you change a model, immediately create and apply a migration. No exceptions.