Reputation: 10476
I have the following objects:
public class DomainStudent {
public long Id { get; set; }
public string AdvisorId { get; set; }
}
public class ApiStudent {
public long Id { get; set; }
public long AdvisorName { get; set; }
}
When I run the following mapping:
ApiStudent api = new ApiStudent();
api.Id = 123;
api.AdvisorName = "Homer Simpson";
DomainStudent existing = service.load(api.Id); // 123
// at this point existing.AdvisorId = 555
existing = Mapper.Map<ApiStudent, DomainStudent>(api);
// at this point existing.AdvisorId = null
How can I configure AutoMapper such that when the property AdvisorId
is missing from the source object, so that it does not get overwritten to null?
Upvotes: 1
Views: 2178
Reputation: 2482
You must change the Map() call to:
Mapper.Map(api, existing);
and then configure the mapping to:
Mapper.CreateMap<ApiStudent, DomainStudent>()
.ForMember(dest => dest.AdvisorId, opt => opt.Ignore());
Upvotes: 3