是否可以使用EntityEntry.Reference加载嵌套的导航属性? [英] Is it possible to load nested navigation properties using EntityEntry.Reference?

查看:52
本文介绍了是否可以使用EntityEntry.Reference加载嵌套的导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择以下示例类:

class TemplatePart
{
    public PartStock stock {get; set;}
    ...other POCOs
}

class PartStock
{
    public Part part {get; set;}
    ...other POCOs
}

class Part
{
    public PartName name {get; set;}
    ...other POCOs
}

现在,假设我已经有一个 TemplatePart 的实体.我可以这样做:

Now, suppose I already have an entity for a TemplatePart. I can do this:

var entry = context.Entry(templatePart);
entry.Reference(x => x.PartStock).Load();

这将加载 PartStock 的导航属性.但是我该怎么做:

That would load the navigation property for the PartStock. But how do I do this:

entry.Reference(x => x.PartStock.Part).Load();

这会产生异常:

表达式'x => x.PartStock.Part'不是有效的属性表达.该表达式应表示一个简单的属性访问:'t => t.MyProperty'.参数名称:propertyAccessExpression

The expression 'x => x.PartStock.Part' is not a valid property expression. The expression should represent a simple property access: 't => t.MyProperty'. Parameter name: propertyAccessExpression

是否有其他替代方法仍然使用我已经拥有的 entry ?如果不需要,我不想使用 Include 重新加载整个事情.

Is there some alternative to this that still uses the entry I already have? I don't want to have to reload the whole thing again using Include if I don't have to.

我正在使用EntityFramework Core 2.

I am using EntityFramework Core 2.

推荐答案

可以直接使用 Query()的组合来代替直接调用 Load 方法> Include / ThenInclude Load 方法:

Instead of directly calling Load method, you could use a combination of Query(), Include / ThenInclude and Load methods:

entry.Reference(x => x.PartStock)
    .Query()
    .Include(x => x.Part)
    .Load();

这篇关于是否可以使用EntityEntry.Reference加载嵌套的导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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