AspNetCore.Docs/aspnetcore/data/ef-rp/read-related-data.md

17 KiB

title author description keywords ms.author manager ms.date ms.topic ms.technology ms.prod uid
Razor Pages with EF Core - Read Related Data - 6 of 10 tdykstra In this tutorial you'll read and display related data -- that is, data that the Entity Framework loads into navigation properties. ASP.NET Core,Entity Framework Core,related data,joins tdykstra wpickett 11/05/2017 get-started-article aspnet asp.net-core data/ef-rp/read-related-data

Reading related data - EF Core with Razor Pages (6 of 10)

By Tom Dykstra and Rick Anderson

[!INCLUDEvalidation]

In this tutorial, related data is is read and displayed. Related data is data that EF loads into navigation properties.

The following illustrations show the completed pages for this tutorial:

Courses Index page

Instructors Index page

There are several ways that EF can load related data into the navigation properties of an entity:

  • Eager loading. When the entity is read, its related data is retrieved. This typically results in a single join query that retrieves all of the data that's needed. Eager loading is specified in EF with the Include and ThenInclude methods.

    Eager loading example

    zz

Rather than a single join query, some of the data can be retrieved in separate queries, and EF "fixes up" the navigation properties. "fixes up" means that EF automatically adds the related entities where they belong. Eager loading with separate queries results in multiple queries sent to the DB.

Consider a query that is currently returning a list, such as ToList or Single. Queries returning a list don't include related data. To include related data, use the Load method instead of a method that returns a list.

Separate queries example

  • Explicit loading. When the entity is first read, related data isn't retrieved. Code is written that retrieves the related data if it's needed. As in the case of eager loading with separate queries, explicit loading results in multiple queries sent to the DB. The difference is that with explicit loading, the code specifies the navigation properties to be loaded. In Entity Framework Core 1.1 you can use the Load method to do explicit loading. For example:

    Explicit loading example

  • Lazy loading. When the entity is first read, related data isn't retrieved. However, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. A query is sent to the DB each time you try to get data from a navigation property for the first time. Entity Framework Core 1.0 does not support lazy loading.

Performance considerations

If you know you need related data for every entity retrieved, eager loading often offers the best performance, because a single query sent to the DB is typically more efficient than separate queries for each entity retrieved. For example, suppose that each department has ten related courses. Eager loading of all related data would result in just a single (join) query and a single round trip to the DB. A separate query for courses for each department would result in eleven round trips to the DB. The extra round trips to the DB are especially detrimental to performance when latency is high.

On the other hand, in some scenarios separate queries is more efficient. Eager loading of all related data in one query might cause a very complex join to be generated, which SQL Server can't process efficiently. Or if you need to access an entity's navigation properties only for a subset of a set of the entities you're processing, separate queries might perform better because eager loading of everything up front would retrieve more data than you need. If performance is critical, it's best to test performance both ways in order to make the best choice.

Create a Courses page that displays Department name

The Course entity includes a navigation property that contains the Department entity of the department that the course is assigned to. To display the name of the assigned department in a list of courses, you need to get the Name property from the Department entity that is in the Course.Department navigation property.

Create a controller named CoursesController for the Course entity type, using the same options for the MVC Controller with views, using Entity Framework scaffolder that you did earlier for the Students controller, as shown in the following illustration:

Add Courses controller

Open CoursesController.cs and examine the Index method. The automatic scaffolding has specified eager loading for the Department navigation property by using the Include method.

Replace the Index method with the following code that uses a more appropriate name for the IQueryable that returns Course entities (courses instead of schoolContext):

[!div class="step-by-step"] Previous