Update an object's unpopulated members with values from another using:
Mapper.Map(customerViewModel).OnTo(customer);
When merging collections, objects are matched by id (configurable if necessary). For example:
var source = new Collection<CustomerViewModel>
{
new CustomerViewModel { Id = 1, Name = "Rod" },
new CustomerViewModel { Id = 2, Name = "Jane" },
new CustomerViewModel { Id = null, Name = "Freddy" }
};
var target = List<Customer>
{
new Customer { CustomerId = 2, Name = null },
new Customer { CustomerId = 1, Name = "Bungle" },
new Customer { CustomerId = 3, Name = "Zippy" }
};
var result = Mapper.Map(source).OnTo(target);
In this case:
source[0]is matched totarget[1], buttarget[1].Nameis already populated, so it isn't changedsource[1]is matched totarget[0], sotarget[0].Nameis changed fromnullto 'Jane'source[2]has no match in the target collection, so a newCustomeris added withCustomerIdset tonullandNameset to 'Freddy'target[2](CustomerId= 3) has no match in thesourcecollection, so it isn't changed- The original
List<Customer>instance is maintained and assigned to theresultvariable