gpt4 book ai didi

asp.net-core-mvc - 包括第二级的一些引用文献

转载 作者:行者123 更新时间:2023-12-03 05:24:00 25 4
gpt4 key购买 nike

假设我们有这个模型:

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,我可以编写如下内容:

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

关于asp.net-core-mvc - 包括第二级的一些引用文献,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com