gpt4 book ai didi

c# - Entity Framework 对象树加载

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:33 24 4
gpt4 key购买 nike

如何加载以下 EF 实体:

alt text

图片来源:http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/20/entity-framework-and-lazy-loading.aspx

假设我们有地址 ID,我们想加载人和宠物的地址。如何做到这一点?

我们可以做到

var address = contex.Addresses.Include("Peson").Where(add => add.Id == GivenId);

但它会加载没有宠物的地址和人员。

如果我包含一个宠物实体,像这样:

var address = contex.Addresses.Include("Peson").Include("Pets").Where(add => add.Id == GivenId);

我得到错误:

指定的包含路径无效。

所以问题是如何加载整个实体树。

最佳答案

您可以通过用“.”分隔关系来加载树

context.Address.Include("Person.Pets"); //Include all the persons with their pets
context.Pets.Include("Person.Address"); //Include all the persons with their addresses

关于c# - Entity Framework 对象树加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2092730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com