The goal here is to ignore null source values, while not requiring the source object to have all the fields the destination object has. Preventing null seems to only work if ALL fields match between objects.
public class ApiStudent {
public long Id { get; set; }
public string Name { get; set; }
}
public class DomainStudent {
public long Id { get; set; }
public string Name { get; set; }
public long SchoolId { get; set; }
}
When I run the following mapping:
Mapper.CreateMap<ApiStudent, DomainStudent>()
.ForAllMembers(opt => opt.Condition(src => !src.IsSourceValueNull));
var api = new ApiStudent();
api.Id = 123;
api.Name = null;
var domain = new DomainStudent();
domain.Id = 123;
domain.Name = "Homer Simpson"; // goal is to prevent this from being written to null
domain = Mapper.Map(api, domain);
// I get an error here saying the SchoolId mapping is missing from ApiStudent
If I remove the ".ForAllMembers(opt => opt.Condition(src => !src.IsSourceValueNull));" from the Mapping definition, I don't get the error, but then the .Name property will be overwritten to null. What am I missing here to get the AutoMapper to skip properties that exist on the destination object but not the source object?