EFCore Linq然后将两个外键包含在同一个表中

时间:2017-09-30 20:17:49

标签: c# asp.net-core linq-to-entities entity-framework-core

有谁看到我做错了什么? ProjectActivityTasksUnitOfMeasureIdProjectActivityTaskTypeId。按照它的编写方式,它认为UnitOfMeasure转到ProjectActivityTaskTypeThenInclude UnitOfMeasure

,这是错误的
  

ProjectActivityTaskType不包含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();

1 个答案:

答案 0 :(得分:7)

您可以(并且应该)重复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();