Two times Projection触发LINQ表达式翻译异常



我正在应用程序,我必须执行投影2次1-在EfModel和DomainModel之间2-Between DomainModel and DTO

我希望从请求到响应的整个过程都是可查询的,所以我执行了以下步骤

1 -在我的graphql查询到达的接口层:

public async Task<IQueryable<BoxDto>> Boxes ([Service] IBoxService boxService, [Service] IMapper mapper)
{
//GetAll as you will see in the next step returns a DomainModel _Box_
//We project it here to a DTO _Exactly here the exception being triggered
return (await boxService.GetAll()).ProjectTo<BoxDto>(mapper.ConfigurationProvider);
}

应用程序层
public async Task<IQueryable<Box>> GetAll()
{
return await _boxRepository.GetAll();
} 

然后在存储库

public async Task<IQueryable<Box>> GetAll()
{
//Here we project from the DBModel _Kisten_ to Box  works smoothly although it's more complex as from DomainModel to DTO
return _dbContext.Kisten.AsNoTracking().ProjectTo<Box>(_mapper.ConfigurationProvider);
}

如果我用<IQueryable<Box>>替换<IQueryable<BoxDto>>,即使使用复杂和嵌套的查询,一切都工作得很好,问题是第二个投影值得分享- BoxDTO和Box是相同的,类型当然不同,但属性有相同的名称-查询需要在正常情况下700毫秒,如果我返回一个DomainModel,而它需要35秒,直到异常出现-我尝试了MaxDepth(1),因为我对循环引用有怀疑,但没有差异

来自AutoMapper的文档:

ProjectTo必须是链中的最后一个呼叫。orm使用实体,而不是dto。因此,在实体上应用任何过滤和排序,并作为最后一步,投影到dto。

这是意料之中的