ASP.NET C#如何将源ID映射到目标对象

问题描述 投票:0回答:1

我对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属性(对象)?预先非常感谢。

c# asp.net-core automapper
1个回答
0
投票
    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));
             }
}

这是我的猜测

© www.soinside.com 2019 - 2024. All rights reserved.