liveBook · Manning

Navigation property

As explained in .NET in Action, Second Edition

A navigation property is an ORM-mapped object or collection property that traverses a relationship to related entities.

A navigation property is an ORM-mapped object or collection property that traverses a relationship to related entities. In Entity Framework Core, navigation properties help represent relationships and perform joins; they can be optional when a related entity is not required. section 5.2.3

Model relationships through corresponding entity properties:

  • Use an object property for a one-to-one relationship, such as Book.Rating and Rating.Book. section 5.2.3
  • Use a collection property for one-to-many relationships, such as Book.Ratings, which EF Core recognizes by convention. section 5.2.3
  • Use collections on both entities for many-to-many relationships, such as Book.Tags and Tag.Books. section 5.2.3

Loading related ratings

Query books with dbContext.Books.Include(b => b.Ratings), then iterate through each book’s Ratings collection to display star ratings and comments. section 5.2.3

Listing 5.8 One-to-one relationship with Book as the parent and Rating as the child
class Book {
  public int Id {get;set;}
  public Rating? Rating {get;}
}
class Rating
{
  public int Id {get;set;}
  public int BookId {get;set;}
  public Book Book {get;set}
}

Inspect the paired Book and Rating properties, including the optional navigation and the BookId foreign key used by convention. section 5.2.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains object, collection, and optional navigations plus foreign-key conventions, but defers explicit-configuration techniques.
Examples
Uses Book, Rating, and Tag entities with collection properties, Include queries, and console output.
What you can do after
Build entity relationships and query related records through EF Core navigation properties.
How it connects
Connects navigation properties with Entity Framework Core, foreign keys, joins, and one-to-one, one-to-many, and many-to-many relationships.

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 .NET in Action, Second Edition. The section links point to the parts of the book it draws on.