liveBook · Manning

Fluent interface

As explained in Building Web APIs with ASP.NET Core

The Entity Framework Core Fluent API is a programmatic configuration interface that defines entity mappings and relationships through chained method calls.

The Entity Framework Core Fluent API configures how C# entities map to a database. Put model configuration in a DbContext subclass’s OnModelCreating method, where ModelBuilder exposes methods that can be chained to describe keys and relationships. section 4.3.3

EF Core also supports conventions and data annotations. Conventions infer mappings from naming patterns; annotations place settings on entity classes. Fluent API settings take precedence when they conflict with either approach. A practical split is to rely on conventions when possible, use annotations for entity-specific settings, and reserve Fluent API for modelwide configuration. section 4.3.3

Configure board-game relationships

For a board-game junction entity, Entity<BoardGames_Domains>().HasKey(...) selects the entity and gives its BoardGameId and DomainId a composite key. Relationship chains then start with HasOne and WithMany, identify the foreign key with HasForeignKey, mark it required with IsRequired, and set cascade deletion with OnDelete. The entity classes also need navigation properties for those relationships. 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 });
}

The OnModelCreating listing shows two junction entities receiving composite keys through ModelBuilder method chains. section 4.3.3

To extend the model, configure a publisher-to-board-game one-to-many relationship or a category-to-board-game many-to-many relationship. In each case, add the relevant entities and navigation properties, then configure foreign keys and cascade rules in the DbContext. section 4.4.2 section 4.4.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Works through composite-key and relationship method chains, then leaves additional one-to-many and many-to-many configurations as exercises.
Examples
Uses board-game junction entities for composite keys and exercises involving publishers and categories.
What it assumes
Assumes familiarity with C# classes and properties while introducing where EF Core model configuration belongs in DbContext.
What you can do after
Build it from scratch in EF Core by configuring composite keys, foreign keys, required relationships, and cascade deletion in OnModelCreating.
How it connects
Connects DbContext, ModelBuilder, database conventions, data annotations, composite keys, navigation properties, foreign keys, and cascade deletion.

Also explained in

Compare how 4 books explain fluent interface →

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.