本文翻译自:Entity Framework - Include Multiple Levels of Properties
The Include() method works quite well for Lists on objects. Include()方法对于对象列表非常有效。 But what if I need to go two levels deep? 但是,如果我需要深入两个层次怎么办? For example, the method below will return ApplicationServers with the included properties shown here. 例如,下面的方法将返回具有此处显示的包含属性的ApplicationServers。 However, ApplicationsWithOverrideGroup is another container that holds other complex objects. 但是,ApplicationsWithOverrideGroup是另一个包含其他复杂对象的容器。 Can I do an Include() on that property as well? 我也可以在该属性上执行Include()吗? Or how can I get that property to fully load? 或如何才能完全加载该属性?
As it stands now, this method: 就目前而言,此方法:
public IEnumerable<ApplicationServer> GetAll()
{
return this.Database.ApplicationServers
.Include(x => x.ApplicationsWithOverrideGroup)
.Include(x => x.ApplicationWithGroupToForceInstallList)
.Include(x => x.CustomVariableGroups)
.ToList();
}
Will populate only the Enabled property (below) and not the Application or CustomVariableGroup properties (below). 将仅填充Enabled属性(如下),而不填充Application或CustomVariableGroup属性。 How do I make this happen? 我如何做到这一点?
public class ApplicationWithOverrideVariableGroup : EntityBase
{
public bool Enabled { get; set; }
public Application Application { get; set; }
public CustomVariableGroup CustomVariableGroup { get; set; }
}
#1楼
参考:https://stackoom.com/question/jPSy/实体框架-包含多个级别的属性
#2楼
If I understand you correctly you are asking about including nested properties. 如果我对您的理解正确,那么您正在询问是否包含嵌套属性。 If so : 如果是这样的话 :
.Include(x => x.ApplicationsWithOverrideGroup.NestedProp)
or 要么
.Include("ApplicationsWithOverrideGroup.NestedProp")
or 要么
.Include($"{nameof(ApplicationsWithOverrideGroup)}.{nameof(NestedProp)}")
#3楼
For EF 6 对于EF 6
using System.Data.Entity;
query.Include(x => x.Collection.Select(y => y.Property))
Make sure to add using System.Data.Entity;
确保using System.Data.Entity;
添加using System.Data.Entity;
to get the version of Include
that takes in a lambda. 获得Include
lambda的Include
版本。
For EF Core 对于EF Core
Use the new method ThenInclude
使用新方法ThenInclude
query.Include(x => x.Collection)
.ThenInclude(x => x.Property);
#4楼
EF Core: Using "ThenInclude" to load mutiple levels: For example: EF核心:使用“ ThenInclude”加载多个级别:例如:
var blogs = context.Blogs
.Include(blog => blog.Posts)
.ThenInclude(post => post.Author)
.ThenInclude(author => author.Photo)
.ToList();
#5楼
I made a little helper for Entity Framework 6 (.Net Core style), to include sub-entities in a nice way. 我为Entity Framework 6(.Net Core样式)提供了一个小帮手,以一种不错的方式包含了子实体。
It is on NuGet now : Install-Package ThenInclude.EF6 现在在NuGet上:Install-Package ThenInclude.EF6
using System.Data.Entity;
var thenInclude = context.One.Include(x => x.Twoes)
.ThenInclude(x=> x.Threes)
.ThenInclude(x=> x.Fours)
.ThenInclude(x=> x.Fives)
.ThenInclude(x => x.Sixes)
.Include(x=> x.Other)
.ToList();
The package is available on GitHub . 该软件包可在GitHub上获得 。
#6楼
I also had to use multiple includes and at 3rd level I needed multiple properties 我还必须使用多个包含,并且在第3层需要多个属性
(from e in context.JobCategorySet
where e.Id == id &&
e.AgencyId == agencyId
select e)
.Include(x => x.JobCategorySkillDetails)
.Include(x => x.Shifts.Select(r => r.Rate).Select(rt => rt.DurationType))
.Include(x => x.Shifts.Select(r => r.Rate).Select(rt => rt.RuleType))
.Include(x => x.Shifts.Select(r => r.Rate).Select(rt => rt.RateType))
.FirstOrDefaultAsync();
This may help someone :) 这可能会帮助某人:)