A database schema defines the structure of its tables and fields. In a SQL-backed application, changing an entity model—for example, adding a field to an order—can require a corresponding schema change. Deployment therefore needs a reliable way to update the database without repeatedly applying old changes. section 9.1 section 9.1.3
Entity Framework migrations record schema changes in order. An initial migration establishes the schema on first deployment; later migrations contain subsequent changes. The database records which migrations have been applied, so redeployment applies only those still outstanding. section 9.1.3
To manage the online shop’s schema:
- Define entity types and a database context, then add Microsoft.EntityFrameworkCore.Design to the SQL-using API project and install the Entity Framework CLI tools. section 9.1.3
- Generate an initial migration with `dotnet ef migrations add InitialCreate`. After adding a nullable `Address` property to an order, generate the next one with `dotnet ef migrations add AddOrderAddress`. section 9.1.3
- Run `context.Database.MigrateAsync(cancellationToken)` during application startup through the database execution strategy, before seeding product data. section 9.1.3
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Address",
table: "Orders",
type: "nvarchar(max)",
nullable: true);
}
The generated `Up` method makes the model change concrete: it adds a nullable `Address` column to the `Orders` table. section 9.1.3
Do not assume migrations will work on a database created with `EnsureCreated()`. That database lacks the table used to track migration versions and needs manual changes before migrations can update it. section 9.1.3











































