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 :-(

2
  • It would be useful to you stackoverflow.com/questions/43947475/… Commented Jun 2, 2019 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. Commented Jun 3, 2019 at 17:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.