0

Is there any option in AutoMappper to only map properties when the target field is null. i.e. only map properties that aren't already set?

I was hoping to do this without having to write an expression for each individual property.

Any help would be appreciated.

Matt

Matt Whetton
  • 6,616
  • 5
  • 36
  • 57

3 Answers3

2

Please try the following solution:

Mapper.CreateMap<TypeA, TypeB>().ForAllMembers(r=>r.Condition(v=>v.DestinationValue==null));

Hope it helps

PinHead877
  • 63
  • 6
  • This was close, the actual mapping was something like this: `cfg.CreateMap() .ForAllMembers(r => r.Condition((source, target, sourceField, targetField) => targetField == null));` – Matt Whetton Sep 11 '16 at 10:36
1

PinHead877 answer did point me very close but the actual answer was more like this:

cfg.CreateMap<TypeA, TypeB>() .ForAllMembers(r => r.Condition((source, target, sourceField, targetField) => targetField == null));
Matt Whetton
  • 6,616
  • 5
  • 36
  • 57
0

Conditional mapping will help you to achieve this.

S.N
  • 4,910
  • 5
  • 31
  • 51