Read Records in Entity Framework Core

Read Records in Entity Framework Core

Reading data from database in Entity Framework Core is quite easy. The below code will load the employee with name ‘Matt’.

using (var context = new CompanyContext())
{
    var emp = context.Employee.Where(e => e.Name == "Matt").FirstOrDefault();
}

There are 2 common ORM patterns used by EF Core to read data.

  • Eager Loading: In Eager Loading the related data is also loaded at the same time.
  • Explicit Loading: In Explicit Loading the related data is not loaded at the same time. It is explicitly loaded from the database at a later time.
Here I will be taking the same database of the Company which contains 2 tables – Employee & Department. There is many-to-one relationship between them, that is a department can have one or more employees.

Eager Loading in EF Core

In Eager Loading the related data is also loaded from the database at the same time, as part of the initial query.

In Eager Loading Include() method is used to read the related entity.

The Employee entity has a Department related entity so the Include() method can be used to do the eager loading of department record like shown below:

using (var context = new CompanyContext())
{
    var emp = context.Employee.Where(e => e.Name == "Matt").Include(s=>s.Department).FirstOrDefault();
}

If you put the breakpoint on VS and check the value of emp variable you will find the value of Department property filled to. I have shown this on the below image:

eager loading of related entity

The Include() method will execute a single SQL query on the database to fetch the data.

This SQL query is given below:

SELECT [e].[Id], [e].[Designation], [e].[Name], [e.Department].[Id], [e.Department].[Name]
FROM [Employee] AS [e]
LEFT JOIN [Department] AS [e.Department] ON [e].[DepartmentId] = [e.Department].[Id]
WHERE [e].[Name] = N'Matt'

Multiple Include() methods

You can also use multiple Include() methods to load multiple related entities.

For example, if the Employee entity also has another related entity Project. Then the following code loads the Department & Project entities of the Employee.

using (var context = new CompanyContext())
{
    var emp = context.Employee.Where(e => e.Name == "Matt")
               .Include(s=>s.Department)
               .Include(s=>s.Project)
               .FirstOrDefault();
}

ThenInclude() Method

Entity Framework core has another method named ThenInclude() that is used to load multiple levels of related entities.

For example suppose I have a related entity named Report of the Department entity.

Then see the below code:

using (var context = new CompanyContext())
{
    var emp = context.Employee.Where(e => e.Name == "Matt")
               .Include(s=>s.Department)
               .ThenInclude(r=>r.Report)
               .FirstOrDefault();
}

.Include(s=>s.Department) will load the Department related entity of the Employee entity.

Next, .ThenInclude(r=>r.Report) will load the Report entity of the Department entity.

Explicit Loading in EF Core

In Explicit Loading the related data is explicitly loaded from the database at a later time. Here the Load() method is used to load related entity explicitly.

Consider the below code:

using (var context = new CompanyContext())
{
    var emp = context.Employee.Where(e => e.Name == "Matt").FirstOrDefault();
    context.Entry(emp).Reference(s => s.Department).Load();
}

The context.Entry(emp).Reference(s => s.Department).Load() loads the Department entity of the Employee. The Reference property gets the reference of related entity and the Load() method loads it explicitly.

If you want to filter the related entity before loading then use the Query() method.

The below code will only load the Department having name as ‘Admin’.

context.Entry(emp).Reference(s => s.Department).Query().Where(s=>s.Name=="Admin");

explicit loading of related entity

DOWNLOAD

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.