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

查看:23
本文介绍了是否可以使用 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/ThenIncludeLoad 方法:

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天全站免登陆