Bốn lệnh cơ bản của dotnet ef:
bash
dotnet ef migrations add AddOrderTable # sinh migration từ diff của model
dotnet ef database update # áp dụng mọi migration đang chờ
dotnet ef migrations list # xem cái đã áp / đang chờ
dotnet ef migrations remove # xoá migration CUỐI (phải chưa áp dụng)Quay lui: database update nhận tên migration đích.
Đưa tên migration trước cái muốn bỏ, EF sẽ chạy phần Down():
bash
dotnet ef database update AddCustomerTable # roll back to this point
dotnet ef migrations remove # now safe to delete the reverted file
dotnet ef database update 0 # revert everythingThứ tự quan trọng: migrations remove chỉ xoá được migration chưa apply. Nếu đã apply mà xoá file trước, model snapshot lệch với DB và lần add sau sẽ sinh migration sai.
Với production, đừng gỡ migration đã chạy — viết một migration mới đảo lại thay đổi. Deploy thường dùng dotnet ef migrations script --idempotent để sinh script SQL cho DBA duyệt thay vì chạy database update trực tiếp.