gpt4 book ai didi

c# - 具有相同类型的嵌套 Dto 的 Dto 失败

转载 作者:太空狗 更新时间:2023-10-30 01:33:38 28 4
gpt4 key购买 nike

我在一个项目中遇到了一个问题,并在一个裸测试项目中成功地重现了它。

我有以下 dto:

public class AppUserDto
{
public int Id { get; set; }
public string Name { get; set; }
}

public class IssueDto
{
public int Id { get; set; }
public AppUserDto Owner { get; set; }
public AppUserDto Creator { get; set; }
}

除了有模型关系而不是 DTO(很明显)之外,对应的模型完全相同。

AutoMapper 配置:

Mapper.CreateMap<AppUser, AppUserDto>().MaxDepth(1);
Mapper.CreateMap<Issue, IssueDto>().MaxDepth(1);

最简单的查询:

var i = context.Issues.ProjectTo<IssueDto>().FirstOrDefault();

这总是抛出 NotSupportedException :

The type 'AppUserDto' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

这当然是automapper的问题。

现在我尝试了以下方法:

 Mapper.CreateMap<AppUser, AppUserDto>().MaxDepth(1)
.ProjectUsing(u => new AppUserDto
{
Id = u == null ? -1 : u.Id,
Name = u == null ? null : u.Name,
});

这使得查询像 context.Issues.ProjectTo<IssueDto>()...成功。但这反过来又为 AppUser 进行了直接映射。导致空值(或 0 对于 Id)。所以context.Users.ProjectTo<AppUserDto>().FirstOrDefault() (甚至 Mapper.Map<AppUserDto>(context.Users.FirstOrDefault()) )总是返回一个 AppUserDto其 Prop 具有默认值。

那么,如何在不牺牲所述 dto 对象的直接映射的情况下使同一类型的多个嵌套 dto 对象在相同的基础 dto 中工作?

用 ProjectUsing 解决问题(如果我们可以同时进行直接映射)不太理想,但如果这是唯一的方法,我可以管理。

编辑:

最有可能存在错误, this is the github issue对于任何感兴趣的人。

最佳答案

罪魁祸首实际上是 MaxDepth 调用本身。看起来可能并非如此,但在每个映射上粘贴 MaxDepth 可能会产生副作用,正如我所见。

事实证明,我的 Dto 根本没有递归(这就是 MaxDepth 的用途)。因此,只需删除所有 MaxDepth 调用即可解决此问题,而无需 ProjectUsing

这已经被清除了here .

关于c# - 具有相同类型的嵌套 Dto 的 Dto 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33144670/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com