【发布时间】:2020-08-05 13:37:14
【问题描述】:
我正在使用 Automapper。在那里,我将 DTO 对象映射到另一个对象。
UserProperties 类。
public string DisplayName { get; set; }
public int Id { get; set; }
public int NotesCount {get;set;}
PersonsDTO 类
public string UserName { get; set; }
public int UserId { get; set; }
public int NotesCount { get ; set; }
public int BooksCount { get; set; }
人物类
public UserProperties? UserDetails { get; set; }
public int NotesCount { get ; set; }
public int BooksCount { get; set; }
在映射配置文件中,
CreateMap<PersonsDTO, Persons>()
.ForPath(o => o.UserDetails.DisplayName, b => b.MapFrom(z => z.UserName))
.ForPath(o => o.UserDetails.Id, b => b.MapFrom(z => z.UserId))
.ReverseMap();
就我而言,Persons 类中的UserDetails 是可空类型。如果PeronsDTO 类中的UserId 是0 表示,我不需要映射UserDetails。对于 UserDetails 属性,它应该返回 null。
我怎样才能实现它?
自动映射器版本:9.0.0
【问题讨论】:
-
有什么解决办法吗?
标签: c# lambda automapper