Load child entity on the fetch of the Parent entity EFCore

blazor c# ef-core-3.0 entity-framework entity-framework-core

Question

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

1
1
1/8/2020 6:16:56 AM

Accepted Answer

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.

1
1/8/2020 6:29:54 AM

Popular Answer

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();


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow