--- name: add-migration description: Changes the AppointmentDesk EF Core model, creates the matching migration with dotnet ef, reviews it for data loss and runs the tests. Use when asked to add, remove, rename or change an entity property, column, table, relationship or index, or to create a migration. argument-hint: "[model change]" --- # Add an EF Core migration The model is in `src/AppointmentDesk.Api/Domain/` and `src/AppointmentDesk.Api/Data/AppDbContext.cs`. Migrations are in `src/AppointmentDesk.Api/Data/Migrations/`. Run every command from the repository root. 1. **Change the model.** Edit the entity class, and `AppDbContext.OnModelCreating` when the column needs a length, index or other configuration (string columns here have a `HasMaxLength`). Change only the entity the user named: `Doctor` and `Patient` both have a `FullName`. For a rename, update every use of the old name (`Data/SeedData.cs`, `Features/`, `tests/`). 2. **Build:** `dotnet build`. Fix errors before going on. 3. **Create the migration** with the tool and a PascalCase name that says what changed: `dotnet ef migrations add --project src/AppointmentDesk.Api -o Data/Migrations` The line "The Entity Framework tools version '10.0.0' is older than that of the runtime" is expected. Ignore it; do not update the tool. 4. **Review the generated `_.cs`, Up and Down, before anything else.** Data loss looks like: - `DropColumn` or `DropTable`; - `AlterColumn` that narrows a type or a length, or makes a column NOT NULL; - `DropColumn` plus `AddColumn` on one table when the user asked for a rename; - the tool printing "An operation was scaffolded that may result in the loss of data". 5. **A rename must be a rename.** If Up drops the old column and adds the new one, undo it with `dotnet ef migrations remove --project src/AppointmentDesk.Api`. Make the rename alone first (put back any other change to that column), create its migration and check that Up says `RenameColumn`; then make the other change and create a second migration. If a rename on its own still comes out as drop and add, replace the pair with `migrationBuilder.RenameColumn(...)` in Up and the reverse in Down. That is the only hand edit allowed in a generated migration; never edit `AppDbContextModelSnapshot.cs`, a `.Designer.cs` file or an older migration. 6. **Other data loss:** if the user asked for it (removing a property, say), name it in the summary. If not, stop and ask before going on. 7. **Build and test:** `dotnet build`, then `dotnet test`. The integration tests apply every migration at startup, so a model change without its migration fails them with `PendingModelChangesWarning`. 8. **Never run `dotnet ef database update`**, or any other `dotnet ef database` command. Applying a migration is a person's decision. 9. **Summarise:** the model change, the migration name and files, what Up and Down do (one line each), any data-loss finding and what you did about it, and the test result.