实体框架 - 包含多级属性 [英] Entity Framework - Include Multiple Levels of Properties

查看:106
本文介绍了实体框架 - 包含多级属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Include()方法对于对象上的列表工作得很好。但是如果我需要深入两级呢?例如,下面的方法将返回ApplicationServers,其中包含的属性如下所示。但是,ApplicationsWithOverrideGroup是另一个容纳其他复杂对象的容器。我可以在该属性上执行Include()吗?或者如何让该物业完全加载?

The Include() method works quite well for Lists on objects. But what if I need to go two levels deep? For example, the method below will return ApplicationServers with the included properties shown here. However, ApplicationsWithOverrideGroup is another container that holds other complex objects. Can I do an Include() on that property as well? Or how can I get that property to fully load?

现在,这种方法:

public IEnumerable<ApplicationServer> GetAll()
{
    return this.Database.ApplicationServers
        .Include(x => x.ApplicationsWithOverrideGroup)                
        .Include(x => x.ApplicationWithGroupToForceInstallList)
        .Include(x => x.CustomVariableGroups)                
        .ToList();
}

只会填充Enabled属性(下面),而不是Application或CustomVariableGroup属性(下面)。如何使这种情况发生?

Will populate only the Enabled property (below) and not the Application or CustomVariableGroup properties (below). 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; }
}


推荐答案

p>

For EF 6

using System.Data.Entity;

query.Include(x => x.Collection.Select(y => y.Property))

请参阅备注更多的例子。

确保使用System.Data.Entity; 添加来获取版本包含,其中包含一个lambda。

Make sure to add using System.Data.Entity; to get the version of Include that takes in a lambda.

如果您使用EF Core可以使用新的方法 然后插入

If you are using EF Core you can use the new method ThenInclude

query.Include(x => x.Collection)
     .ThenInclude(x => x.Property);

这篇关于实体框架 - 包含多级属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆