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



