EF核心嵌套选择对于第三层实体为空

时间:2019-05-08 19:57:01

标签: entity-framework-core

我具有以下数据库结构:

User --> Membership <-- Org <-- Basic (basic info)
                         ^----- Contacts (org contacts)
                         ^----- Invoice
                                   ^----- Contacts (invoice contacts)

以下查询返回Invoice.Contacts = null

var userOrgs = await _context.Memberships
    .Include(p => p.User)
    .Include(p => p.Org)
        .ThenInclude(p => p.Basic)
    .Include(p => p.Org)
        .ThenInclude(p => p.Contacts)
    .Include(p => p.Org)
        .ThenInclude(p => p.Invoice)
            .ThenInclude(p => p.Contacts)
    .Where(p => p.User.Id == request.UserId)
    .Select(p => new GetUserOrgsResponse
    {
        OrgId = p.Org.Id,
        Basic = p.Org.Basic,
        Contacts = p.Org.Contacts,
        Invoice = p.Org.Invoice,  // <<-- result will have Invoice.Contacts = null
        InsertDate = p.InsertDate,
        Roles = p.Roles.Select(s => s.ToString().ToLower()).ToArray()
    })
    .ToListAsync(cancellationToken);

如果不使用,一切都很好 .Select(p => new GetUserOrgsResponse { ... }),但只离开.ToListAsync(cancellationToken)

我应该如何解决此查询以返回还包含Invoice.Contacts(第三级实体)的GetUserOrgsResponse列表?

0 个答案:

没有答案