【问题标题】:How can I use ThenInclude on a 3rd level off of a many to many object?如何在多对多对象的第三层使用 ThenInclude?
【发布时间】:2018-04-21 00:18:57
【问题描述】:

我有这个问题:

IQueryable<Comment> comments = _commentRepo.Comments
            .Include(c => c.CommentStaff)
            .ThenInclude(c => c.StaffOffices)
            .Where(c => c.CommentAuditId == auditId)
            .OrderByDescending(c => c.CommentDate).Take(3);

StaffOffices 是一个多对多桥查找 POCO,如下所示:

[Table("staff_office")]
public class StaffOffice
{
    [Column("staff_office_staff_id")]
    public short ID { get; set; }
    public Staff Staff { get; set; }

    [Column("staff_office_office_id")]
    public short OfficeID { get; set; }
    public Office Office { get; set; }
}

我需要通过此查询填充 Office 对象。

所以我正在尝试这个:

如您所见,在 Intellisense 中,Office 并没有出现在 StaffOffices 中的 ThenInclude 中。

我怎样才能让它工作? 这是否需要在 AppContext 模型中使用 fluent API 定义?

【问题讨论】:

标签: entity-framework-core


【解决方案1】:

有时即使它没有在智能感知中显示,它也会起作用。你可以试试。此外,如果您需要员工数据,则必须再次包含 StaffOffices。

IQueryable<Comment> comments = _commentRepo.Comments
            .Include(c => c.CommentStaff)
            .ThenInclude(c => c.StaffOffices)
            .ThenInclude(c => c.Office)
            .ThenInclude(c => c.StaffOffices)
            .ThenInclude(c => c.Staff)
            .Where(c => c.CommentAuditId == auditId)
            .OrderByDescending(c => c.CommentDate).Take(3);

如果您选择评论人员回购,多对多级别将是第二级。虽然订购可能很难立即完成。然后,您将不得不从 cmets 变量中订购和获取。

例如:

var comments = _commentStaffRepo.CommentStaff
            .Include(c => c.StaffOffices)
            .ThenInclude(c => c.Office)
            .Include(c => c.StaffOffices)
            .ThenInclude(c => c.Staff)
            .Include(c => c.Comments)
            .Where(c => c.Comments.Any(co => co.CommentAuditId == auditId))
            .Select(c => c.Comments);
comments = comments.OrderByDescending(c => c.CommentDate).Take(3);

【讨论】:

    【解决方案2】:

    这是一个已知问题,IntelliSense 不适用于多个级别。你只要输入Office,它就会构建成功。

    您可以参考以下错误。

    823793744117

    【讨论】:

    • 是的 - 谢谢! - 如果我输入它,它会编译并且红色波浪形消失。试着看看我现在能不能完成剩下的解决方案:)
    • @Sam 当然,如果您需要帮助,请告诉我
    • @Sam 对你有用吗?如果是,请您接受我的回答。
    【解决方案3】:

    感谢 Vivek 和上面的评论。

    这是我现在的组件调用方法:

    public IViewComponentResult Invoke(int auditId)
        {
            IQueryable<Comment> comments = _commentRepo.Comments
                .Include(c => c.CommentStaff)
                .ThenInclude(c => c.StaffOffices).ThenInclude(so => so.Office)
                .Where(c => c.CommentAuditId == auditId)
                .OrderByDescending(c => c.CommentDate).Take(3);
    
            CommentVM commentVM = new CommentVM
            {
                AuditId = auditId,
                Comments = comments
            };
    
            return View(commentVM);
        }
    

    我做了另一个关卡 ThenInclude:

    .ThenInclude(c => c.StaffOffices).ThenInclude(so => so.Office)
    

    这是 Razor 中的 sn-p 说明:

                            <div class="row">
                                <div class="col-xs-6">
                                    <i>@comment.CommentStaff.FullName </i>
                                    @if (comment.CommentStaff.StaffOffices.Count() > 1)
                                    {
                                    <span>@($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")</span>
                                    }
                                </div>
                                <div class="col-xs-6">
                                    @comment.CommentDate
                                </div>
                            </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多