--- title: Razor Pages with EF Core in ASP.NET Core - Read Related Data - 6 of 8 author: rick-anderson description: In this tutorial you read and display related data -- that is, data that the Entity Framework loads into navigation properties. ms.author: riande ms.custom: mvc ms.date: 10/24/2018 uid: data/ef-rp/read-related-data --- # Razor Pages with EF Core in ASP.NET Core - Read Related Data - 6 of 8 By [Tom Dykstra](https://github.com/tdykstra), [Jon P Smith](https://twitter.com/thereformedprog), and [Rick Anderson](https://twitter.com/RickAndMSFT) [!INCLUDE [about the series](../../includes/RP-EF/intro.md)] In this tutorial, related data is read and displayed. Related data is data that EF Core loads into navigation properties. If you run into problems you can't solve, [download or view the completed app.](https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/ef-rp/intro/samples) [Download instructions](xref:index#how-to-download-a-sample). 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 Core can load related data into the navigation properties of an entity: * [Eager loading](/ef/core/querying/related-data#eager-loading). Eager loading is when a query for one type of entity also loads related entities. 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. EF Core will issue multiple queries for some types of eager loading. Issuing multiple queries can be more efficient than was the case for some queries in EF6 where there was a single query. Eager loading is specified with the `Include` and `ThenInclude` methods. ![Eager loading example](read-related-data/_static/eager-loading.png) Eager loading sends multiple queries when a collection navigation is included: * One query for the main query * One query for each collection "edge" in the load tree. * Separate queries with `Load`: The data can be retrieved in separate queries, and EF Core "fixes up" the navigation properties. "fixes up" means that EF Core automatically populates the navigation properties. Separate queries with `Load` is more like explict loading than eager loading. ![Separate queries example](read-related-data/_static/separate-queries.png) Note: EF Core automatically fixes up navigation properties to any other entities that were previously loaded into the context instance. Even if the data for a navigation property is *not* explicitly included, the property may still be populated if some or all of the related entities were previously loaded. * [Explicit loading](/ef/core/querying/related-data#explicit-loading). When the entity is first read, related data isn't retrieved. Code must be written to retrieve the related data when it's needed. Explicit loading with separate queries results in multiple queries sent to the DB. With explicit loading, the code specifies the navigation properties to be loaded. Use the `Load` method to do explicit loading. For example: ![Explicit loading example](read-related-data/_static/explicit-loading.png) * [Lazy loading](/ef/core/querying/related-data#lazy-loading). [Lazy loading was added to EF Core in version 2.1](/ef/core/querying/related-data#lazy-loading). When the entity is first read, related data isn't retrieved. The first time a navigation property is accessed, the data required for that navigation property is automatically retrieved. A query is sent to the DB each time a navigation property is accessed for the first time. * The `Select` operator loads only the related data needed. ## Create a Course page that displays department name The Course entity includes a navigation property that contains the `Department` entity. The `Department` entity contains the department that the course is assigned to. To display the name of the assigned department in a list of courses: * Get the `Name` property from the `Department` entity. * The `Department` entity comes from the `Course.Department` navigation property. ![Course.Department](read-related-data/_static/dep-crs.png) ### Scaffold the Course model # [Visual Studio](#tab/visual-studio) Follow the instructions in [Scaffold the student model](xref:data/ef-rp/intro#scaffold-the-student-model) and use `Course` for the model class. # [.NET Core CLI](#tab/netcore-cli) Run the following command: ```console dotnet aspnet-codegenerator razorpage -m Course -dc SchoolContext -udl -outDir Pages\Courses --referenceScriptLibraries ``` --- The preceding command scaffolds the `Course` model. Open the project in Visual Studio. Open *Pages/Courses/Index.cshtml.cs* and examine the `OnGetAsync` method. The scaffolding engine specified eager loading for the `Department` navigation property. The `Include` method specifies eager loading. Run the app and select the **Courses** link. The department column displays the `DepartmentID`, which isn't useful. Update the `OnGetAsync` method with the following code: [!code-csharp[](intro/samples/cu/Pages/Courses/Index.cshtml.cs?name=snippet_RevisedIndexMethod)] The preceding code adds `AsNoTracking`. `AsNoTracking` improves performance because the entities returned are not tracked. The entities are not tracked because they're not updated in the current context. Update *Pages/Courses/Index.cshtml* with the following highlighted markup: [!code-html[](intro/samples/cu/Pages/Courses/Index.cshtml?highlight=4,7,15-17,34-36,44)] The following changes have been made to the scaffolded code: * Changed the heading from Index to Courses. * Added a **Number** column that shows the `CourseID` property value. By default, primary keys aren't scaffolded because normally they're meaningless to end users. However, in this case the primary key is meaningful. * Changed the **Department** column to display the department name. The code displays the `Name` property of the `Department` entity that's loaded into the `Department` navigation property: ```html @Html.DisplayFor(modelItem => item.Department.Name) ``` Run the app and select the **Courses** tab to see the list with department names. ![Courses Index page](read-related-data/_static/courses-index.png) ### Loading related data with Select The `OnGetAsync` method loads related data with the `Include` method: [!code-csharp[](intro/samples/cu/Pages/Courses/Index.cshtml.cs?name=snippet_RevisedIndexMethod&highlight=4)] The `Select` operator loads only the related data needed. For single items, like the `Department.Name` it uses a SQL INNER JOIN. For collections, it uses another database access, but so does the `Include` operator on collections. The following code loads related data with the `Select` method: [!code-csharp[](intro/samples/cu/Pages/Courses/IndexSelect.cshtml.cs?name=snippet_RevisedIndexMethod&highlight=4)] The `CourseViewModel`: [!code-csharp[](intro/samples/cu/Models/SchoolViewModels/CourseViewModel.cs?name=snippet)] See [IndexSelect.cshtml](https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/IndexSelect.cshtml) and [IndexSelect.cshtml.cs](https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/data/ef-rp/intro/samples/cu/Pages/Courses/IndexSelect.cshtml.cs) for a complete example. ## Create an Instructors page that shows Courses and Enrollments In this section, the Instructors page is created. ![Instructors Index page](read-related-data/_static/instructors-index.png) This page reads and displays related data in the following ways: * The list of instructors displays related data from the `OfficeAssignment` entity (Office in the preceding image). The `Instructor` and `OfficeAssignment` entities are in a one-to-zero-or-one relationship. Eager loading is used for the `OfficeAssignment` entities. Eager loading is typically more efficient when the related data needs to be displayed. In this case, office assignments for the instructors are displayed. * When the user selects an instructor (Harui in the preceding image), related `Course` entities are displayed. The `Instructor` and `Course` entities are in a many-to-many relationship. Eager loading is used for the `Course` entities and their related `Department` entities. In this case, separate queries might be more efficient because only courses for the selected instructor are needed. This example shows how to use eager loading for navigation properties in entities that are in navigation properties. * When the user selects a course (Chemistry in the preceding image), related data from the `Enrollments` entity is displayed. In the preceding image, student name and grade are displayed. The `Course` and `Enrollment` entities are in a one-to-many relationship. ### Create a view model for the Instructor Index view The instructors page shows data from three different tables. A view model is created that includes the three entities representing the three tables. In the *SchoolViewModels* folder, create *InstructorIndexData.cs* with the following code: [!code-csharp[](intro/samples/cu/Models/SchoolViewModels/InstructorIndexData.cs)] ### Scaffold the Instructor model # [Visual Studio](#tab/visual-studio) Follow the instructions in [Scaffold the student model](xref:data/ef-rp/intro#scaffold-the-student-model) and use `Instructor` for the model class. # [.NET Core CLI](#tab/netcore-cli) Run the following command: ```console dotnet aspnet-codegenerator razorpage -m Instructor -dc SchoolContext -udl -outDir Pages\Instructors --referenceScriptLibraries ``` --- The preceding command scaffolds the `Instructor` model. Run the app and navigate to the instructors page. Replace *Pages/Instructors/Index.cshtml.cs* with the following code: [!code-csharp[](intro/samples/cu/Pages/Instructors/Index1.cshtml.cs?name=snippet_all&highlight=2,18-99)] The `OnGetAsync` method accepts optional route data for the ID of the selected instructor. Examine the query in the *Pages/Instructors/Index.cshtml.cs* file: [!code-csharp[](intro/samples/cu/Pages/Instructors/Index1.cshtml.cs?name=snippet_ThenInclude)] The query has two includes: * `OfficeAssignment`: Displayed in the [instructors view](#IP). * `CourseAssignments`: Which brings in the courses taught. ### Update the instructors Index page Update *Pages/Instructors/Index.cshtml* with the following markup: [!code-html[](intro/samples/cu/Pages/Instructors/IndexRRD.cshtml?range=1-65&highlight=1,5,8,16-21,25-32,43-57)] The preceding markup makes the following changes: * Updates the `page` directive from `@page` to `@page "{id:int?}"`. `"{id:int?}"` is a route template. The route template changes integer query strings in the URL to route data. For example, clicking on the **Select** link for an instructor with only the `@page` directive produces a URL like the following: `http://localhost:1234/Instructors?id=2` When the page directive is `@page "{id:int?}"`, the previous URL is: `http://localhost:1234/Instructors/2` * Page title is **Instructors**. * Added an **Office** column that displays `item.OfficeAssignment.Location` only if `item.OfficeAssignment` isn't null. Because this is a one-to-zero-or-one relationship, there might not be a related OfficeAssignment entity. ```html @if (item.OfficeAssignment != null) { @item.OfficeAssignment.Location } ``` * Added a **Courses** column that displays courses taught by each instructor. See [Explicit Line Transition with `@:`](xref:mvc/views/razor#explicit-line-transition-with-) for more about this razor syntax. * Added code that dynamically adds `class="success"` to the `tr` element of the selected instructor. This sets a background color for the selected row using a Bootstrap class. ```html string selectedRow = ""; if (item.CourseID == Model.CourseID) { selectedRow = "success"; }