Include()方法对于对象列表非常有效。但是,如果我需要深入两个层次怎么办?例如,下面的方法将返回具有此处显示的包含属性的ApplicationServers。但是,ApplicationsWithOverrideGroup是另一个包含其他复杂对象的容器。我也可以在该属性上执行Include()吗?或如何才能完全加载该属性?
就目前而言,此方法:
public IEnumerable<ApplicationServer> GetAll() { return this.Database.ApplicationServers .Include(x => x.ApplicationsWithOverrideGroup) .Include(x => x.ApplicationWithGroupToForceInstallList) .Include(x => x.CustomVariableGroups) .ToList(); }
将仅填充Enabled属性(如下),而不填充Application或CustomVariableGroup属性。我如何做到这一点?
public class ApplicationWithOverrideVariableGroup : EntityBase { public bool Enabled { get; set; } public Application Application { get; set; } public CustomVariableGroup CustomVariableGroup { get; set; } }
对于EF 6
using System.Data.Entity; query.Include(x => x.Collection.Select(y => y.Property))
确保添加using System.Data.Entity;以获取Includelambda 的版本。
using System.Data.Entity;
Include
对于EF Core
使用新方法 ThenInclude
ThenInclude
query.Include(x => x.Collection) .ThenInclude(x => x.Property);