liveBook · Manning

Schema

As explained in Building Distributed Applications that Work

A database schema is the structure and organization of a database, including its tables and fields.

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

How Building Distributed Applications that Work covers it

How deep it goes
Works through migration generation and startup application, while leaving manual conversion of EnsureCreated databases unspecified.
Examples
Shows an order address property becoming a nullable column through a generated migration.
What it assumes
Requires an Entity Framework database context, at least one entity type, and CLI tools before generating a migration.
What you can do after
Apply it via Entity Framework migrations to update a SQL schema when deploying an Aspire application.
How it connects
Connects database schema to entity models, Entity Framework migrations, deployment, and data seeding.

Also explained in

Compare how 44 books explain schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.