I’m using this Using DynamicMap() and ignore null source value approach to make the mapper ignore properties with NULL values on the source, but this doesn’t appear to work in version 2.1.267.0 (current).
Here’s my mapping config:
Mapper.CreateMap<PersonDTO, Person>().ForAllMembers(
opt => opt.Condition(srs => !srs.IsSourceValueNull));
Here’s my test:
// Arrange
var Person = new Person { FirstName = "Bruce", Surname = "Lee"};
var PersonDto = new PersonDTO { FirstName = "Jet", Surname = null };
// Act
Mapper.Map(PersonDto, Person);
// Assert
Assert.AreEqual("Jet", Person.FirstName); // Assert.AreEqual failed. Expected:<Jet>. Actual:<Bruce>.
Assert.AreEqual("Lee", Person.Surname, "Surname field with NULL value should not have been mapped");
Does it work for you? And if so, can you spot the problem in my code?
This is supposed to be fixed in AutoMapper version 2.2