run-migration
Generate và apply EF Core migration cho một microservice
SKILL.md.claude/skills/dev/be/run-migration/SKILL.md
GitHubRun EF Core Migration $ARGUMENTS
Usage: /run-migration ServiceName MigrationName
Example: /run-migration BatteryService AddBattery
Generate Migration
cd services/{Service}/src/{Service}.Api
dotnet ef migrations add {MigrationName} -p ../{Service}.Infrastructure -s .
Flags:
-p(project): Infrastructure project (nơi DbContext)-s(startup): Api project (nơi Program.cs)
Apply Migration
dotnet ef database update -p ../{Service}.Infrastructure -s .
Verify
ls -la ../{Service}.Infrastructure/Persistence/Migrations/
Migration Name Convention
| Action | Pattern | Example |
|---|---|---|
| Entity mới | Add{Entity} |
AddBattery |
| Thêm field | Add{Field}To{Entity} |
AddVoltageThresholdToBattery |
| Xóa field | Remove{Field}From{Entity} |
RemoveOldFieldFromBattery |
| Schema ban đầu | InitialCreate |
InitialCreate |
| Relationship | Add{A}{B}Relation |
AddBatteryTicketRelation |
Troubleshooting
# Xóa migration cuối (nếu chưa apply)
dotnet ef migrations remove -p ../{Service}.Infrastructure -s .
# Xem danh sách migrations
dotnet ef migrations list -p ../{Service}.Infrastructure -s .
# Generate SQL script để review
dotnet ef migrations script -p ../{Service}.Infrastructure -s .
Rules
- LUÔN chạy từ Api project directory
- Migration name: PascalCase, mô tả rõ ràng
- Review migration file trước khi apply
- Auto-migrate on startup (
db.Database.Migrate()trong Program.cs)