migration
Creating and applying an EF Core database migration in any backend microservice. Triggers when a new Entity is added, a column is added/renamed/removed, a DbSet is added to ApplicationDbContext, or af
Files1
View on GitHubSKILL.md.codex/skills/be/migration/SKILL.md
GitHubNguyên tắc bắt buộc
- Tuân thủ tuyệt đối quy trình và checklist được định nghĩa trong skill này — không bỏ qua bước rollback test, không dùng
DROP TABLEtrongUp(). - Hỏi trước khi chạy nếu bất kỳ yêu cầu nào chưa rõ (tên migration, có data cần seed không, bảng nào đang có data production). Không đoán mò — một câu hỏi ngắn tốt hơn một migration phá data.
BE Migration Pattern
Create migration
# Run from Api project directory
dotnet ef migrations add {MigrationName} -p ../ServiceName.Infrastructure -s .
Migration name must describe the change:
AddBatteryTableAddBatteryStatusColumnCreateTicketTableRenameLocationCodeColumn
Apply migration
dotnet ef database update -p ../ServiceName.Infrastructure -s .
Rollback test (required when schema changes)
dotnet ef database update {PreviousMigrationName} -p ../ServiceName.Infrastructure -s .
dotnet ef database update -p ../ServiceName.Infrastructure -s . # re-apply
NOT NULL column on existing table — seed first
// In Up() — populate before adding constraint
migrationBuilder.Sql("UPDATE batteries SET status = 1 WHERE status IS NULL");
migrationBuilder.AlterColumn<int>("status", "batteries", nullable: false, defaultValue: 1);
Checklist before commit
- Migration name clearly describes the change
-
Down()method exists and works - NOT NULL columns have
defaultValueor data seeding - Rename uses
RenameColumn, not drop + recreate - Rollback tested:
update [prev]→updatesucceeds - No
DROP TABLEorTRUNCATEinUp()/Down()
TimescaleDB vs PostgreSQL
- Sensor readings, time-series → TimescaleDB (hypertable)
- Users, tickets, configs, battery configs → PostgreSQL