然后,EFCore Linq将两个外键包含到同一表中 [英] EFCore Linq ThenInclude Two Foreign Keys To Same Table

查看:447
本文介绍了然后,EFCore Linq将两个外键包含到同一表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人看到我在做什么错吗?
ProjectActivityTasks具有UnitOfMeasureIdProjectActivityTaskTypeId.通过编写方式,它认为UnitOfMeasure转到ProjectActivityTaskType.在ThenInclude上错误地表示UnitOfMeasure的说法

Does anyone see what I am doing wrong?
ProjectActivityTasks has the UnitOfMeasureId and the ProjectActivityTaskTypeId. With the way it is written, it thinks that UnitOfMeasure goes to ProjectActivityTaskType. It is erroring out on the ThenInclude for UnitOfMeasure saying

ProjectActivityTaskType不包含UnitOfMeasure的定义

ProjectActivityTaskType does not contain a definition for UnitOfMeasure

这是正确的. UnitOfMeasure转到ProjectActivityTasks.

which is correct. UnitOfMeasure goes to ProjectActivityTasks.

我正在引用此页面,但它似乎无法以这种方式工作:

I was referencing this page but it does not seem to work this way: 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)部分:

You can (and should) repeat the Include(x => x.ProjectActivityTasks) part:

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();

这篇关于然后,EFCore Linq将两个外键包含到同一表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆