如何使用实体框架加载多对多对象图
本文关键字:多对象 对象图 加载 何使用 实体 框架 | 更新日期: 2023-09-27 18:34:12
我有一个多对多关系(在本例中为"左"右"和"合并"(和另一个键入"左"的实体"卫星"。碰巧的是,Sattelite的FK也有一个独特的索引。我的目标是加载一个 Joinder 实体及其 Left 和 Right 实体,使用 where 子句的附属属性。
我尝试了许多方法,但我对 Linq 的词汇量很弱,我甚至不知道我正在寻找什么术语。
var joinder = dbContext.Joinders
.Include(j => j.Left)
.Include(j => j.Right)
.Include(j => j.Left.Satellites)
.FirstOrDefault(s => s.Name == "Hubble");
这不起作用,因为 FirstOrDefault 子句没有 s 的上下文来分析名称。
var joinder = dbContext.Joinders
.Include(j => j.Left)
.Include(j => j.Right)
.Include(j => j.Left.Satellites)
.Select(j => j.Left.Satellites)
.Where(s => s.Name == "Hubble");
这不起作用,因为来自 Select 的类型是IQueryable<Collection<Satellite>>
这令人困惑。
var query = from j in dbContext.Joinders
join l in dbContext.Lefts on j.LeftId equals l.Id
join r in dbContext.Rights on j.RightId equals r.Id
join s in dbContext.Satellites on l.Id equals s.LeftId
where s.Name == "Hubble"
select j;
此查询编译并运行,但向我返回完全冻结的对象 - 我返回的 Joinder 引用具有 Left 和 Right 属性均为 null。
var query = from j in dbContext.Joinders
join l in dbContext.Lefts on j.LeftId equals l.Id
join r in dbContext.Rights on j.RightId equals r.Id
join s in dbContext.Satellites on l.Id equals s.LeftId
where s.Name == "Hubble"
select new Joinder
{
Left = l,
Right = r,
Left.Satellites = ...?
};
这似乎不起作用,因为我似乎无法在自动初始值设定项中取消引用这些属性名称。
有人知道如何做到这一点吗?本质上,我想搜索"实体框架多对多深度负载",但我想不是每个人都会像我一样说它。
var joinder = dbContext.Joinders
.Include(j => j.Right)
.Include(j => j.Left.Satellites)
.FirstOrDefault(j => j.Left.Satellites.Any(s => s.Name == "Hubble"));
它返回至少具有一个具有给定名称的卫星的第一个连接程序。 .Include(j => j.Left.Satellites)
还将包括Left
实体(指向最后一个属性的路径上的所有内容(,因此不需要单独的Include(j => j.Left)
。
编辑
如果您不想将相关Satellites
与Joinder
一起加载,只需将.Include(j => j.Left.Satellites)
替换为.Include(j => j.Left)
即可。FirstOrDefault
中的谓词(取决于Satellite
属性(仍然有效。
试试这个
var joinders = dbContext.Joinders
.Include(j => j.Left)
.Include(j => j.Right)
.Include(j => j.Left.Satellites)
.Where(j => j.Left.Satellites.Any(s => s.Name == "Hubble");
这将返回所有Joinders,其中任何链接到"左"的卫星都有名称"哈勃"。我知道左派和右派会急于加载,但对于卫星,我不确定。