Automapper: Ignore on condition of

The Ignore() feature is strictly for members you never map, as these members are also skipped in configuration validation. I checked a couple of options, but it doesn't look like things like a custom value resolver will do the trick.

Use the Condition() feature to map the member when the condition is true:

Mapper.CreateMap<CarViewModel, Car>()
 .ForMember(dest => dest.Code, opt => opt.Condition(source => source.Id != 0))

I ran into a similar issue, and while this will overwrite the existing value for dest.Code with null, it might be helpful as a starting point:

AutoMapper.Mapper.CreateMap().ForMember(dest => dest.Code,config => config.MapFrom(source => source.Id != 0 ? null : source.Code));

Tags:

C#

Automapper