在父实体 EFCore 的获取上加载子实体 [英] Load child entity on the fetch of the Parent entity EFCore

查看:26
本文介绍了在父实体 EFCore 的获取上加载子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号.在使用 find 方法从数据库中获取时,用子实体加载父实体的更好方法是什么?

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?

父实体:

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; }
}

子实体:

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

    public string FirstLine { get; set; }

    public string SecondLine { get; set; }

    public string Province { get; set; }
}

现在,当我尝试使用 Find 方法获取数据时,我得到的地址实体为 null,但是当我在子表中检查该 ID 的数据库数据时.

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.

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

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

推荐答案

如你所说:

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

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

是的!这是在 EF Core 中加载相关数据的最佳方式.

你进一步说:

我已经使用了 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
}

并且您必须按如下方式注册您的 DbConext:

And you have to register your DbConext as follows:

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

UseLazyLoadingProxies() 方法在 Microsoft 中可用.EntityFrameworkCore.Proxies nuget 包.

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

注意:您不能为某个查询禁用延迟加载.所以使用 Eager loading 是在 EF Core 中加载相关数据的最佳方式.

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.

这篇关于在父实体 EFCore 的获取上加载子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆