liveBook · Manning

Include

As explained in .NET in Action, Second Edition

Include is an Entity Framework Core query extension method that specifies related entities to load with the queried entities.

Include is an Entity Framework Core query extension method that eagerly loads a related entity with the queried entity. Add a separate Include for each relationship that must be present in the returned objects. section 5.2.3

Loading books with ratings

Query the Books set and include each book's Ratings collection: dbContext.Books.Include(b => b.Ratings). The result can then print every book and its ratings without separately retrieving the related collection. 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}
}

The related navigation-property model shows the relationship that Include makes available during eager loading. section 5.2.3

How .NET in Action, Second Edition covers it

How deep it goes
Explains separate Includes, eager-versus-lazy loading, and alternative loading approaches without detailing query translation internals.
Examples
Demonstrates loading Book entities with their Ratings collection and printing the results to the console.
What it assumes
Assumes entity relationships and navigation properties, while distinguishing eager loading from lazy loading.
What you can do after
Apply it via Entity Framework Core to retrieve books together with their ratings in one eager-loading query.
How it connects
Connects eager loading with relationships, lazy loading, explicit loading, and select loading.

Also explained in

Compare how 2 books explain Include →

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.