liveBook · Manning

Navigation property

As explained in Building Web APIs with ASP.NET Core

A navigation property is an entity property that represents an associated entity or collection of entities in an object-relational model.

A navigation property is an entity property that directly references one related entity or a collection of related entities. In EF Core, it provides an object-level way to work with relationships between database records. section 4.3.3

For the board-game model, navigation properties connect BoardGame with Domain, Mechanic, and their junction entities. Add reciprocal properties to both sides, then configure the relationships so EF Core can populate the references. section 4.3.3

Use the property shape that matches the relationship:

  • Use a nullable entity property for a direct reference, such as BoardGame? or Domain? on a junction entity. section 4.3.3
  • Use a nullable ICollection<T> property for the related collection, such as BoardGames_Domains on BoardGame or Domain. section 4.3.3
  • Configure each reference with the Fluent API, linking HasOne, WithMany, HasForeignKey, requiredness, and delete behavior. section 4.3.3

Board games and domains

A BoardGames_Domains junction entity can expose BoardGame? BoardGame and Domain? Domain. BoardGame and Domain can each expose ICollection<BoardGames_Domains>? BoardGames_Domains. Fluent API then maps BoardGameId and DomainId as foreign keys and configures cascading deletes. section 4.3.3

Publisher relationship

A one-to-many Publisher–BoardGame relationship uses navigation properties on both entities, a required PublisherId foreign key on BoardGame, cascading rules, and a Publisher DbSet in ApplicationDbContext. section 4.4.2

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
 
       modelBuilder.Entity<BoardGames_Domains>()
           .HasKey(i => new { i.BoardGameId, i.DomainId });
 
modelBuilder.Entity<BoardGames_Domains>()
.HasOne(x => x.BoardGame)
.WithMany(y => y.BoardGames_Domains)
.HasForeignKey(f => f.BoardGameId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
 
modelBuilder.Entity<BoardGames_Domains>()
.HasOne(o => o.Domain)
.WithMany(m => m.BoardGames_Domains)
.HasForeignKey(f => f.DomainId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
 
       modelBuilder.Entity<BoardGames_Mechanics>()
           .HasKey(i => new { i.BoardGameId, i.MechanicId });
 
modelBuilder.Entity<BoardGames_Mechanics>()
.HasOne(x => x.BoardGame)
.WithMany(y => y.BoardGames_Mechanics)
.HasForeignKey(f => f.BoardGameId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
 
modelBuilder.Entity<BoardGames_Mechanics>()
.HasOne(o => o.Mechanic)
.WithMany(m => m.BoardGames_Mechanics)
.HasForeignKey(f => f.MechanicId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
}

Inspect the Fluent API configuration that connects junction navigation properties with foreign keys, collections, required relationships, and cascading deletes. section 4.3.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains single and collection properties, reciprocal declarations, and Fluent API mapping, stopping short of query-loading behavior.
Examples
Uses BoardGame, Domain, Mechanic, Publisher, Category, and junction entities to demonstrate one-to-many and many-to-many mappings.
What it assumes
Assumes entity classes and junction entities, while introducing navigation properties and relationship configuration through concrete board-game examples.
What you can do after
Build it from scratch by adding reciprocal properties and configuring their foreign-key relationships with the EF Core Fluent API.
How it connects
Ties navigation properties to EF Core, DbContext, Fluent API, foreign keys, junction entities, and relationship cardinality.

Also explained in

Compare how 3 books explain navigation property →

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.