I'm using automapper v11 (C# net6) I would like to Ignore(not map) all null values for all fields.
Here an example
class SrcExample
{
public string? Name { get; set; }
public int? Age { get; set; }
}
class DestExample
{
public string Name { get; set; }
public int Age { get; set; }
}
var map = CreateMap<SrcExample, DestExample>();
map.ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
I'm trying to Ignore(not map) all null values using opts.Condition
but it only works for strings.
For int? I got srcMember = 0
instead of srcMember = null
. Seems like Automapper uses default int value as srcMember value.
Why Automapper behaves this way?
Is there a way (using automapper) to Ignore(not map) all null values for all fields.
This question (How to ignore null values for all source members during mapping in Automapper 6?) do not resolve the problem, due to the set
method on the destination class is called anyway, (Here an example of how set
method is called https://dotnetfiddle.net/1grZkZ) I want to avoid set
method being called. I want to completely ignore the map when the source value is null.