How to automap this(mapping sub members)

前端 未结 4 1993
春和景丽
春和景丽 2021-01-17 13:52

I have something like this

public class ProductViewModel
{
  public int SelectedProductId { get; set; }
  public string ProductName {get; set;}
  public int          


        
4条回答
  •  没有蜡笔的小新
    2021-01-17 14:11

    The correct answer given by allrameest on this question should help: AutoMapper - Deep level mapping

    This is what you need:

    Mapper.CreateMap()
        .ForMember(dest => dest.Product, opt => opt.MapFrom(src => src));
    Mapper.CreateMap()
        .ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.SelectedProductId));
    

    NOTE: You should try to move away from using Mapper.CreateMap at this point, it is obsolete and will be unsupported soon.

提交回复
热议问题