在第二级包括几个参考 [英] Include several references on the second level

查看:26
本文介绍了在第二级包括几个参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有这个模型:

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 表等中检索所有数据.使用 Include/ThenInclude API,我可以编写如下内容:

With EF7 I would like to retrieve all data from the Tiers table, with data from the Contact table, from the Titre table, from the TypeContact table and so on ... with one single instruction. With Include/ThenInclude API I can write something like this :

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

但是在 Titre 属性之后,我不能包含其他引用,例如 TypeContact、Langue、Fonction ... Include 方法建议使用 Tiers 对象,然后 ThenInclude 建议使用 Titre 对象,而不是 Contact 对象.如何包含我的联系人列表中的所有引用?我们可以通过一条指令实现这一目标吗?

But after Titre property, I can't include others references like TypeContact, Langue, Fonction ... Include method suggests a Tiers objects, and ThenInclude suggests a Titre object, but not a Contact object. How can I include all references from my list of Contact? Can we achieve this with one single instruction?

推荐答案

.ThenInclude() 将链接最后一个 .ThenInclude() 或最后一个 .Include() (以较新的为准)以拉入多个级别.要在同一级别包含多个同级,只需使用另一个 .Include() 链.正确格式化代码可以大大提高可读性.

.ThenInclude() will chain off of either the last .ThenInclude() or the last .Include() (whichever is more recent) to pull in multiple levels. To include multiple siblings at the same level, just use another .Include() chain. Formatting the code right can drastically improve readability.

_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.

这篇关于在第二级包括几个参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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