Automapper: bidirectional mapping with ReverseMap() and ForMember()

后端 未结 2 1839
野的像风
野的像风 2020-12-04 18:35

I have the case where I want to map an entity to a viewmodel and back. I have to specify the mapping explicitly with ForMember() because their properties do not

2条回答
  •  孤城傲影
    2020-12-04 19:17

    You could define your configuration like this:

    Mapper.CreateMap()
        .ForMember(dst => dst.PartInteger, opt => opt.MapFrom(src => src.Integer));
    
    Mapper.CreateMap()
        .ForMember(dst => dst.Integer, opt => opt.MapFrom(src => src.PartInteger));
    

    UPDATE

    Here is the commit where ReverseMap was initially implemented. From what I can see in the code, it only creates a simple reverse mapping. For example, in this case it would automatically configure the equivalent of:

    Mapper.CreateMap();
    

    To get anything more complex, I'm afraid that you're going to have to configure it manually.

提交回复
热议问题