加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core-mvc – 在第二级包含几个引用

发布时间:2020-12-16 00:38:52 所属栏目:asp.Net 来源:网络整理
导读:假设我们有这个模型: public class Tiers{ public ListContact Contacts { get; set; }} 和 public class Contact{ public int Id { get; set; } public Tiers Tiers { get; set; } public Titre Titre { get; set; } public TypeContact TypeContact { get
假设我们有这个模型:
public class Tiers
{
    public List<Contact> Contacts { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    public Tiers Tiers { get; set; }
    public Titre Titre { get; set; }
    public TypeContact TypeContact { get; set; }
    public Langue Langue { get; set; }
    public Fonction Fonction { get; set; }
    public Service Service { get; set; }
    public StatutMail StatutMail { get; set; }
}

使用EF7,我想从Tiers表中检索所有数据,其中包含Contact表中的数据,Titre表,TypeContact表等等,其中包含一条指令。 With Include / ThenInclude API我可以写这样的东西:

_dbSet
     .Include(tiers => tiers.Contacts)
          .ThenInclude(contact => contact.Titre)
     .ToList();

但是在Titre属性之后,我不能包含其他引用,如TypeContact,Langue,Fonction … Include方法建议一个Tiers对象,ThenInclude建议一个Titre对象,但不是一个Contact对象。如何包括我的联系人列表中的所有引用?我们可以用一个指令来实现吗?

解决方法

.ThenInclude()将链接最后一个.ThenInclude()或最后一个.Include()(以较新的为准)来拉入多个级别。要在同一级别包含多个兄弟姐妹,只需使用另一个.Include()链。将代码格式化可以大大提高可读性。
_dbSet
    .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.Titre)
    .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.TypeContact)
    .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.Langue);
    // etc.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读