Monday, March 30, 2020

Entity Framework Core Eager, Explicity and Lazy Loading

Lazy loading Performance issues

Lazy loading allows us to reduce the size of the Main entity object in a LINQ query which has references to related entity objects. When the query executes it fetched the data of only the main entity object, data for the related objects are loaded only when we access the Navigational property from the main table.

With this approach we might end us firing multiple SQL queries to the underlying database and eventually bring down the performance. Let us assume we have a Grid in the View which displays a list of employees and each item has a details link which will display employee data and its related entity department’s data, then when the page loads a SQL query related to the employee’s entity will execute and get the list of employees. Later when the user clicks on each employee details link separate SQL queries will get executed to get each employees department data as follows.

Lazy loading in Entity Framework Core

In Lazy Loading the data from the Main table gets loaded first in the initial load, but the data from the related tables are not loaded, they get loaded only when we access the Navigational property from the main table. To perform Lazy loading we need to enable Lazy Loading while initializing the DBContext.

Lazy Loading was introduced in EF Core 2.1. To enable lazy loading we need to install the Microsoft.EntityFrameworkCore.Proxies package and enable it with a call to UseLazyLoadingProxies.

Let us start by installing the Microsoft.EntityFrameworkCore.Proxies package.

Explicit loading in Entity Framework Core

Explicit loading is a kind of lazy loading but the dependent objects are not loaded automatically, but we will have to explicitly load the dependent objects whenever needed. We have to use the Load() method to load related entities explicitly.

The initial data is loaded using a regular context query, the related data is loaded either using Reference for a single object or a Collection to load multiple related objects. Let us take the same Employee Department structure. First let us load the Employee details and later load the corresponding Department details using explicit loading.

Following are the LINQ queries to load employee and related department details using explicit loading.

Eager Loading in Entity Framework Core

Eager loading loads the data from the main table and the related / dependent tables together during the initial load. To perform eager loading we use the Include() method. Let us consider our sample tables Employee and Department to implement Eager loading.

The Employee table which has a Foreign Key DepartmentId referencing to the Department table. We will load the data from the Employee table and also load the corresponding data from the Department table using Eager Loading.

EFCore - Loading Dependent table data

When we Generate the Model classes using Entity Framework Core it creates Navigational properties for the Model classes which have a Foreign Key relation. Using these Navigational properties we can load dependent data in different ways.

In our previous examples we have seen the Employee table has Foreign Key relation with the Department table and the Country table, when we generate the Model classes using Entity Framework Core the Employee class will have reference to the related classes, the Employee model class will be as follows.