liveBook · Manning

Code-first approach

As explained in Building Web APIs with ASP.NET Core

Code-first is a development approach that defines a data model in application code and derives or updates a database schema from it.

Code-first starts with C# entity classes and uses them to generate a database schema. An entity represents a kind of record; the schema defines the database’s tables, fields, relationships, and constraints. In EF Core, migrations keep the schema in sync as the model changes. Database-first reverses that direction: it generates classes from an existing schema. section 4.3.2 section 4.0

To use code-first for a board-game API:

  • Install the EF Core database provider and design package, then install and check the dotnet-ef CLI tool. section 4.3.2
  • Define a DbContext and entity classes for board games, domains, mechanics, and their relationships. Use conventions where possible, data annotations for entity settings, and the Fluent API for model-wide settings. section 4.3.3
  • Run `dotnet ef migrations add Initial` to generate the initial migration, then `dotnet ef database update Initial` to apply it. Adding a migration creates files; it does not update the database. section 4.3.5

Model a board game

A BoardGame class can mirror fields the API needs from a CSV file. Data annotations specify details such as the table name, primary key, required fields, and maximum name length. Separate entity classes represent domains and mechanics; relationship entities connect them to board games. section 4.3.3

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
 
modelBuilder.Entity<BoardGames_Domains>()
.HasKey(i => new { i.BoardGameId, i.DomainId });
 
modelBuilder.Entity<BoardGames_Mechanics>()
.HasKey(i => new { i.BoardGameId, i.MechanicId });
}

Composite keys for the relationship entities are configured in DbContext with the Fluent API. section 4.3.3

Check that the initial migration and model snapshot appear in `/Migrations/`, then verify the tables after applying the migration. EF Core updates the snapshot with later migrations so it can calculate changes needed to keep the schema aligned with the model. section 4.3.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through entity configuration, initial migration creation, database update, and verification without detailing generated SQL.
Examples
Models board games, domains, mechanics, and relationship entities, then generates and applies an initial migration.
What it assumes
Introduces data model and schema terminology while building on an existing .NET web API project.
What you can do after
Build it from scratch using EF Core entities and migrations to create and update a database schema.
How it connects
Connects entity classes, DbContext, data annotations, the Fluent API, database schemas, and EF Core migrations.

Also explained in

Compare how 4 books explain code-first approach →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.