问题描述
假设我们有这个模型:
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.
这篇关于在第二层包括一些参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!