EFCore Linq ThenInclude 两个相同的外键 Table

EFCore Linq ThenInclude Two Foreign Keys To Same Table

有没有人看出我做错了什么?
ProjectActivityTasksUnitOfMeasureIdProjectActivityTaskTypeId。按照它的写法,它认为 UnitOfMeasure 转到 ProjectActivityTaskType。它在 ThenInclude 上出错,因为 UnitOfMeasure

ProjectActivityTaskType does not contain a definition for UnitOfMeasure

这是正确的。 UnitOfMeasure 转到 ProjectActivityTasks

我引用了这个页面,但它似乎不是这样工作的:https://docs.microsoft.com/en-us/ef/core/querying/related-data

var qry = await _projectActivityRepository.GetAll()
.Include(x => x.ProjectActivityVehicles)
  .ThenInclude(x => x.Vehicle)
.Include(x => x.ProjectActivityTasks)
  .ThenInclude(x => x.ProjectActivityTaskType)
  .ThenInclude(x => x.UnitOfMeasure)
.Where(x => x.Id == Id && x.TenantId == (int)AbpSession.TenantId)
.FirstOrDefaultAsync();

您可以(并且应该)重复 Include(x => x.ProjectActivityTasks) 部分:

var qry = await _projectActivityRepository.GetAll()
.Include(x => x.ProjectActivityVehicles)
  .ThenInclude(x => x.Vehicle)
.Include(x => x.ProjectActivityTasks)
  .ThenInclude(x => x.ProjectActivityTaskType)
.Include(x => x.ProjectActivityTasks)
  .ThenInclude(x => x.UnitOfMeasure)
.Where(x => x.Id == Id && x.TenantId == (int)AbpSession.TenantId)
.FirstOrDefaultAsync();