I am combining objects that have a List element. I tell AutoMapper to ignore elements with source code, but when I merge into an object with a zero set, the receiver gets an empty collection (even if it has elements in front of the map).
Any ideas on how to prevent this?
ConfigurationInfo template1 = new ConfigurationInfo() {
Columns = null
};
ConfigurationInfo template2 = new ConfigurationInfo() {
Columns = new List<ColumnInfo>()
};
template2.Columns.AddRange(existingColumns);
ConfigurationInfo template3 = new ConfigurationInfo() {
Columns = null
};
var config = new AutoMapper.MapperConfiguration(cfg => {
cfg.AllowNullCollections = true;
cfg.AllowNullDestinationValues = true;
cfg.CreateMap<ConfigurationInfo, ConfigurationInfo>()
.ForAllMembers(option => {
option.Condition((source, destination, sourceMember, destMember) => sourceMember != null);
});
});
var mapper = config.CreateMapper();
ConfigurationInfo finalTemplate = new ConfigurationInfo();
mapper.Map(template1, finalTemplate);
mapper.Map(template2, finalTemplate);
mapper.Map(template3, finalTemplate);
source
share