Automapper: map properties manually

前端 未结 3 1207
轻奢々
轻奢々 2021-02-07 07:11

I just started to use automapper to map DTOs<->Entities and it seems to be working great.

In some special cases I want to map only some properties and perform additio

3条回答
  •  梦毁少年i
    2021-02-07 07:16

    If you just want to map only some select property than you have to do as below

    // Create a map
    var map = CreateMap();
    // ingnore all existing binding of property
    map.ForAllMembers(opt => opt.Ignore());
    // than map property as following
    map.ForMember(dest => dest.prop1, opt => opt.MapFrom( src => src.prop1));
    map.ForMember(dest => dest.prop2, opt => opt.MapFrom( src => src.prop2));
    

提交回复
热议问题