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

C# 具有相同类型的嵌套Dto的Dto失败,c#,asp.net-mvc,automapper,automapper-4,C#,Asp.net Mvc,Automapper,Automapper 4,我在一个项目中遇到了一个问题,并在一个简单的测试项目中成功地重新编程 我有以下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

我在一个项目中遇到了一个问题,并在一个简单的测试项目中成功地重新编程

我有以下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(显然)

自动映射配置:

Mapper.CreateMap<AppUser, AppUserDto>().MaxDepth(1);
Mapper.CreateMap<Issue, IssueDto>().MaxDepth(1);
这当然是automapper的问题

现在我尝试了以下方法:

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

Mapper.CreateMap供感兴趣的人使用。

罪魁祸首实际上是
MaxDepth
调用本身。看起来可能不是这样,但在每个贴图上粘贴
MaxDepth
可能会产生副作用,正如我所看到的

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


这已被清除。

我完全不明白您为什么要使用MaxDepth。这些是用于递归DTO的。你的模型中没有递归的东西。是的,这已经被清除了。谢谢你,吉米。
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.
 Mapper.CreateMap<AppUser, AppUserDto>().MaxDepth(1)
       .ProjectUsing(u => new AppUserDto
       {
            Id = u == null ? -1 : u.Id,
            Name = u == null ? null : u.Name,
       });