문제

I'm a little confuse about the direction of the mapping.

This is the create mapping.

Mapper.CreateMap<Dimension, PostedDimensionViewModel>()
            .ForMember(dto => dto.inputDimension, opt => opt.MapFrom(p => p.DimensionName))
            .ForMember(dto => dto.inputDescription, opt => opt.MapFrom(p => p.Description));

Then I can use it this way to go from PostedDimensionViewModel => Dimension:

Dimension dimension = Mapper.Map<PostedDimensionViewModel, Dimension>(model);

What if, I want to go from Dimension => PostedDimensionViewModel, do I need another CreateMap or I can just use the same above mapping from both directions?

Thanks for helping.

도움이 되었습니까?

해결책

Just do this:

Mapper.CreateMap<Source, Destination>()
    /* your other stuff here */
    .ReverseMap();

There are limitations here, and I wouldn't recommend this except between two DTOs.

다른 팁

Yes, you'll need to provide an additional map if you intend to go in the other direction. If you try to use AutoMapper without the extra map you'll get an exception:

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:

PostedDimensionViewModel -> Dimension

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top