Automapper ignore null values

0

I'm using AutoMapper, the point is that I do not want to map the null values, since I want to keep the value of the target entity members. I've done it this way but it does not work.

opts.Condition((source, dest, sourceMember, destMember)=> sourceMember !=null ));
    
asked by TheOligarch 06.12.2018 в 01:44
source

1 answer

0

Let's start from the base that what you map is the structure, the null is data, although if you can validate in the mapping if there is certain data take an action, but the property must be defined because it is part of the object

Conditional Mapping

cfg.CreateMap<classOrigen,classDestino>()
     .ForMember(dest => dest.Prop1, opt => opt.Condition(src => (src.Prop1 != null)));

in this case in both classes you have defined Prop1 but only apply according to the value imagine

public class classDestino{
    public int Prop1 {get;set;}
}

public class classOrigen{
    public int? Prop1 {get;set;}
}

As you will see, a class allows null but the destination does not, so that it does not fail, you add the condition

    
answered by 06.12.2018 в 02:03