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

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

问题描述

有人看到我做错了什么吗?
ProjectActivityTasks 具有 UnitOfMeasureIdProjectActivityTaskTypeId.按照它的编写方式,它认为 UnitOfMeasure 转到 ProjectActivityTaskType.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.

我正在引用此页面,但它似乎无法以这种方式工作:https://docs.microsoft.com/en-us/ef/core/querying/related-data

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天全站免登陆