AutoMapper: "Ignore the rest"?
From what I understood the question was that there are fields on the destination which doesn't have a mapped field in the source, which is why you are looking for ways to Ignore those non mapped destination fields.
Instead of implementing and using these extension method you could simply use
Mapper.CreateMap<sourceModel, destinationModel>(MemberList.Source);
Now the automapper knows that it needs to only validate that all the source fields are mapped but not the other way around.
You can also use:
Mapper.CreateMap<sourceModel, destinationModel>(MemberList.Destination);
I've updated Can Gencer's extension to not overwrite any existing maps.
public static IMappingExpression<TSource, TDestination>
IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof (TSource);
var destinationType = typeof (TDestination);
var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));
foreach (var property in existingMaps.GetUnmappedPropertyNames())
{
expression.ForMember(property, opt => opt.Ignore());
}
return expression;
}
Usage:
Mapper.CreateMap<SourceType, DestinationType>()
.ForMember(prop => x.Property, opt => opt.MapFrom(src => src.OtherProperty))
.IgnoreAllNonExisting();