Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load child entity on the fetch of the Parent entity EFCore

I have the below model. What is the better way to load the parent entity with child entity at the time of fetching from the DB with find method?

Parent Entity:

public class Client
{
    public int Id { get; set; }

    public string LastName { get; set; }

    public string Gender { get; set; }

    public DateTime DateOfBirth { get; set; }

    public Address Address { get; set; }
}

Child Entity:

public class Address
{
    public int Id { get; set; }

    public string FirstLine { get; set; }

    public string SecondLine { get; set; }

    public string Province { get; set; }
}

Now when I try to fetch the data using the Find method I got the address entity null, but when I check in the DB data exist for that ID in Child table too.

referenceContext.Clients.Find(client.Id);

Is there a way to overcome this? When I fetch the parent object and at the same time the value of the child entity is also loaded with the parent.

Notes: As of now, if I used the Include(i => i.Address) then, and then, only I am able to load the child entity.

I already use the Include but is there any other option exist to load child entity if I get the parent entity.

referenceContext.Clients.Where(c => c.IsActive.Equals(true))
                        .Include(i => i.Address).ToList();

DB Deatils

like image 455
KARAN Avatar asked Jan 01 '23 09:01

KARAN


2 Answers

As you said:

Notes: As of now, if I used the Include(i => i.Address) then, and then, only I am able to load the child entity.

Yes! this is the best way to load related data in EF Core.

You further said:

I already use the Include but is there any other option exist to load child entity if I get the parent entity.

Yes! There is! That is called Lazy loading. To enable lazy loading you have to make the navigation property virtual as follows:

public class Client
{
    public int Id { get; set; }

    public string LastName { get; set; }

    public string Gender { get; set; }

    public DateTime DateOfBirth { get; set; }

    public virtual Address Address { get; set; } // <-- Here it is
}

And you have to register your DbConext as follows:

services.AddDbContext<BloggingContext>(
    b => b.UseLazyLoadingProxies() // <-- Here is it is
          .UseSqlServer(myConnectionString));

UseLazyLoadingProxies() method is available in the Microsoft.EntityFrameworkCore.Proxies nuget package.

Note: You cannot disable lazy loading for a certain query. So using Eager loading is the best way to load related data in EF Core.

like image 129
TanvirArjel Avatar answered Jan 04 '23 16:01

TanvirArjel


In EF, there is a concept called Eager Loading using .Include.

MS Docs - Loading Related Data - EF Core

.NET Fiddle

using MyContext context = new MyContext();

IList<Client> clients =
    context.Clients
        .Include(c => c.Address)
        .Where(c => c.LastName == "patel")
        .ToList();
like image 27
Indar Avatar answered Jan 04 '23 17:01

Indar