我对automapper非常陌生,我正在尝试将DTO对象映射到应保存在数据库中的实体。这是我的示例代码:
public class SourceObject {
public int Id {get; set;}
public string Description {get; set;}
public int StatusId {get; set;}
}
public class DestinationObject {
public int Id {get; set;}
public string Description {get; set;}
public DestinationStatus Status {get; set;}
}
public DestinationStatus {
public int Id {get; set;}
public string Description {get; set;}
}
我如何为AutoMapper定义MappingProfile类,以将SourceObject的输入StatusId属性(简单类型)映射到DestinationObject的DestinationStatus属性(对象)?预先非常感谢。
public class MappingProfile : Profile
{
public MappingProfile()
{
this.CreateMap<SourceObject, DestinationObject>()
.ForMember(n => n.Id, opt => opt.MapFrom(n => n.Id))
.ForMember(n => n.Description, opt => opt.MapFrom(n => n.Description))
.ForMember(n => n.Status, opt => opt.MapFrom(n => new DestinationStatus(){ Id = n.Id, Description = n.Description));
}
}
这是我的猜测