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