--- title: Razor Pages with EF Core - Read Related Data - 6 of 10 author: tdykstra description: In this tutorial you'll read and display related data -- that is, data that the Entity Framework loads into navigation properties. keywords: ASP.NET Core,Entity Framework Core,related data,joins ms.author: tdykstra manager: wpickett ms.date: 11/05/2017 ms.topic: get-started-article ms.technology: aspnet ms.prod: asp.net-core uid: data/ef-rp/read-related-data --- # Reading related data - EF Core with Razor Pages (6 of 10) By [Tom Dykstra](https://github.com/tdykstra) and [Rick Anderson](https://twitter.com/RickAndMSFT) [!INCLUDE[validation](../../includes/RP-EF/intro.md)] 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](read-related-data/_static/courses-index.png) ![Instructors Index page](read-related-data/_static/instructors-index.png) ## Eager, explicit, and lazy Loading of related data 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](read-related-data/_static/eager-loading.png) 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](read-related-data/_static/separate-queries.png) * 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](read-related-data/_static/explicit-loading.png) * 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](read-related-data/_static/add-courses-controller.png) 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](xref:data/ef-rp/complex-data-model)