简体   繁体   English

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

[英]Dto with nested Dtos of the same type fails

I faced a problem in a project and have successfully repro'd it in a bare test project. 我在一个项目中遇到了一个问题,并在一个裸测试项目中成功地重新编写了它。

I have the following dtos: 我有以下dtos:

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; }
}

The corresponding models are absolutely the same except that there are model relationships instead of DTOs (obviously). 除了有模型关系而不是DTO(显然)之外,相应的模型是完全相同的。

AutoMapper config: AutoMapper配置:

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

The simplest of queries: 最简单的查询:

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

This always throws a NotSupportedException : 这总是抛出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.

This is a problem from automapper of course. 这当然是来自automapper的一个问题。

Now I tried the following: 现在我尝试了以下内容:

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

This makes queries like context.Issues.ProjectTo<IssueDto>()... succeed. 这使得context.Issues.ProjectTo<IssueDto>()...等查询成功。 But this in turns make direct mappings for AppUser result in null values (or 0 for Id). 但这反过来使得AppUser直接映射导致空值(或Id为​​0)。 So context.Users.ProjectTo<AppUserDto>().FirstOrDefault() (or even Mapper.Map<AppUserDto>(context.Users.FirstOrDefault()) ) always return an AppUserDto with default values for its props. 所以context.Users.ProjectTo<AppUserDto>().FirstOrDefault() (甚至Mapper.Map<AppUserDto>(context.Users.FirstOrDefault()) )总是返回一个AppUserDto ,其默认值为其props。

So , how to make multiple nested dto objects of the same type in the same base dto work without sacrificing direct mappings for said dto object? 那么 ,如何在同一个基础dto中创建相同类型的多个嵌套dto对象,而不牺牲所述dto对象的直接映射?

Solving the problem with ProjectUsing (if we can make direct mappings work at the same time) is less than ideal, but if it's the only way to go I can manage. 使用ProjectUsing解决问题(如果我们可以让直接映射同时工作)不太理想,但如果这是唯一的方法我可以管理。

EDIT: 编辑:

There is a bug most likely, this is the github issue for anyone who's interested. 最有可能存在一个错误, 这是任何感兴趣的人 的github问题

The culprit was actually the MaxDepth call itself. 罪魁祸首实际上是MaxDepth电话本身。 It may not seem so but sticking MaxDepth on every mapping might produce side effects as I've seen. 它可能看起来不是这样,但在每个映射上粘贴MaxDepth可能会产生副作用,正如我所见。

As it turns out, I don't have recursion on my Dtos at all (and that's what MaxDepth is for). 事实证明,我根本没有对我的Dtos进行递归(这就是MaxDepth的用途)。 So just removing all MaxDepth calls will solve this without needing ProjectUsing . 因此,只需删除所有MaxDepth调用即可解决此问题而无需ProjectUsing

This has been cleared out here . 这已经在这里清除了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM