假设我们有这个模型:
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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。