I'm trying to map 2 objects of the same type.
What I want to do is AutoMapper to igonore all the properties, that have Null
value in the source object, and keep the existing value in the destination object.
I've tried using this in my "Repository", but it doesn't seem to work.
Mapper.CreateMap<TEntity, TEntity>().ForAllMembers(p => p.Condition(c => !c.IsSourceValueNull));
What might be the problem ?
So far I've solved it like this.
but still hoping to find a solution using AutoMapper
Taking Marty's solution (which works) one step further, here is a generic method to make it easier to use:
Usage:
Condition
overload with 3 parameters let's you make expression equivalent to your examplep.Condition(c => !c.IsSourceValueNull)
:Method signature:
Equivalent expression:
Interesting, but your original attempt should be the way to go. Below test is green:
Work Around - Add DataMember property in destination type [DataMember(EmitDefaultValue = false)] will remove the property if the source value is null.
if the Role value is null Role property will be removed.