问题描述
选择以下示例类:
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();
这会产生异常:
是否有其他替代方法仍然使用我已经拥有的 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加载嵌套的导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!