简体   繁体   English

在获取父实体 EFCore 时加载子实体

[英]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?在使用 find 方法从数据库中获取时,用子实体加载父实体的更好方法是什么?

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.现在,当我尝试使用 Find 方法获取数据时,我得到的地址实体为空,但是当我在 Child 表中也存在该 ID 的数据库数据时。

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.注意:截至目前,如果我使用Include(i => i.Address)然后,只有我能够加载子实体。

I already use the Include but is there any other option exist to load child entity if I get the parent entity.我已经使用了 Include 但是如果我得到父实体,是否还有其他选项可以加载子实体。

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

数据库详细信息

In EF, there is a concept called Eager Loading using .Include .在 EF 中,有一个使用.Include称为Eager Loading的概念。

MS Docs - Loading Related Data - EF Core MS Docs - 加载相关数据 - EF Core

.NET Fiddle .NET 小提琴

using MyContext context = new MyContext();

IList<Client> clients =
    context.Clients
        .Include(c => c.Address)
        .Where(c => c.LastName == "patel")
        .ToList();

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.注意:截至目前,如果我使用Include(i => i.Address)然后,只有我能够加载子实体。

Yes!是的! this is the best way to load related data in EF Core.这是在 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.我已经使用了 Include 但是如果我得到父实体,是否还有其他选项可以加载子实体。

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:您必须按如下方式注册您的DbConext

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

UseLazyLoadingProxies() method is available in the Microsoft.EntityFrameworkCore.Proxies nuget package. UseLazyLoadingProxies()方法在Microsoft.EntityFrameworkCore.Proxies nuget 包中可用。

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.所以使用Eager loading是在 EF Core 中加载相关数据的最佳方式。

You can use Include()您可以使用Include()

Linq query Linq 查询

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

Lambda Expression Lambda 表达式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM