How to use AutoMapper .ForMember?

后端 未结 5 1289
野性不改
野性不改 2020-12-04 15:10

I am trying to set up AutoMapper to convert from Entity to DTO. I know I\'m supposed to be using .ForMember() after Mapper.CreateMap()<

5条回答
  •  执笔经年
    2020-12-04 15:30

    a sample implementation would be as follows:

    Mapper.CreateMap()
      .ForMember(m => m.GameType, opt => opt.MapFrom(src => src.Type))
    

    We need to map this property since the names of the properties of Game and GameViewModel are different - if they are the same and of the same type then it will not need a ForMember

    another use of the ForMember is to Ignore Mappings

    Mapper.CreateMap()
        .ForMember(dest => dest.Prize, opt => opt.Ignore());
    

提交回复
热议问题