3

This seems like such a common Automapper problem that I feel like I 100% must be missing it even though I've scoured the net. If I have source and destination objects:

class Source {
   int? price { get; set; }
}

class Destination {
   int price { get;set; }
}

CreateMap<Source, Destination>()
     .ForAllMembers(o => o.Condition((source, destination, member) => 
              (member != null) ));

Will not work because the nullable property will still use the default for that type, meaning the Destination.price gets set to 0 every time, even when Source.price.HasValue==false

Given most DTO's use nullable fields to prevent users from sending more fields than required, the question is:

How do we globally configure Automapper 8.1 to NOT MAP any nullable properties when HasValue==false (i.e. leave the current value of Destination.price as-is).

Again, I've spent hours scouring documentation and example but am clearly missing this.. sorry if its obvious :-(

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
Feech
  • 439
  • 1
  • 6
  • 14
  • It would be useful to you https://stackoverflow.com/questions/43947475/how-to-ignore-null-values-for-all-source-members-during-mapping-in-automapper-6 – Ali Soltani Jun 02 '19 at 11:10
  • 2
    I don't see how that solution does anything to prevent Automapper from mapping nullable TYPES correctly. All this does is deal with the standard "don't map if source is null". It doesn't deal with "don't map if the nullabletype.hasvalue==false. I'm really dumbfounded how this problem is not front and center.. thanks for your suggestion though, much appreciated. – Feech Jun 03 '19 at 17:06

0 Answers0