Search code examples
c#automapperautomapping

AutoMapper only map when target field is null


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


Solution

  • 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));