Load child entity on the fetch of the Parent entity EFCore

筅森魡賤 提交于 2020-11-29 09:39:11

问题


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


回答1:


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.




回答2:


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



回答3:


You can use Include()

Linq query

using (var context = new DBEntities())
{
   var result = (from c in context.Client.Include("Address")
            where c.IsActive
            select c).ToList();
}

Lambda Expression

using (var context = new DBEntities())
{
   var result = context.Client.Include(p => p.Address).Where(c => c.IsActive).ToList();
}


来源:https://stackoverflow.com/questions/59639245/load-child-entity-on-the-fetch-of-the-parent-entity-efcore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!