简体   繁体   English

实体框架 - 在相关实体上加载相关实体(一对多对一)

[英]Entity Framework - Load related entity on related entites (one to many to one)

I'm trying to load an object (a) and that object has a collection of objects (b), each object:b has single objects attached to them.我正在尝试加载一个对象 (a) 并且该对象具有一组对象 (b),每个对象:b 都有单个对象附加到它们。 I've cut down the code below to show you how it's setup.我已经减少了下面的代码来向您展示它是如何设置的。

I can without any problems load 'MyImprovement'.我可以毫无问题地加载“MyImprovement”。 I can in that same moment load the collection of 'MyCondition'.我可以在同一时刻加载“MyCondition”的集合。 But what I fail to load is 'MyComponent' that is referenced in the 'MyCondition'...但是我未能加载的是“MyCondition”中引用的“MyComponent”...

I managed to find some samples using .Include and .ThenInclude, however, .ThenInclude does not exist from what I can see?我设法使用 .Include 和 .ThenInclude 找到了一些样本,但是,从我所见,.ThenInclude 不存在?

I've tried every possible aspect of this and I still think that it should be possible to do,... right?我已经尝试了这一切可能的方面,但我仍然认为应该可以做到,......对吗?

Anyone who's up for the task?有谁来接任务吗? I'm completely lost right now, so please help!我现在完全迷失了,所以请帮助! Thanks in advance!提前致谢!

/Karl /卡尔

public class MyImprovement
{
    [Key]
    public int MyImprovementId { get; set; }
    public virtual ICollection<MyCondition> Conditions { get; set; }
}

public class MyCondition
{
    [Key]
    public int MyConditionId { get; set; }
    public int? ComponentId { get; set; }
    public virtual MyComponent ConditionalMyComponent { get; set; }
}

public class MyComponent
{
    [Key]
    public int ComponentId { get; set; }
    public string PDNumber { get; set; }
}     

// Code to load the first 2 levels of objects
MyImprovement improvement = dbx.MyImprovements
                                  .Include("Conditions")
                                  .Where(x => x.ImprovementId == id)
                                        .First();

If you want to eager-load complex entity graph, you can combine multiple .Include()- Methods with following style:如果你想预先加载复杂的实体图,你可以组合多个 .Include()- 方法,具有以下样式:

MyImprovement improvement = dbx.MyImprovements
                                  .Include("Conditions")
                                  .Include("Conditions.ConditionalMyComponent")
                                  .Where(x => x.ImprovementId == id)
                                        .First();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM