Skip to main content

A couple useful AutoMapper extension methods of ignoring some, or all properties.

//
// A more elegant auto-mapper ignore https://stackoverflow.com/a/16808867
public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(
    this IMappingExpression<TSource, TDestination> map,
    Expression<Func<TDestination, object>> selector)
{
    map.ForMember(selector, config => config.Ignore());
    return map;
}

//
// Usage:
Mapper.CreateMap<JsonRecord, DatabaseRecord>()
    .Ignore(record => record.Field)
    .Ignore(record => record.AnotherField)
    .Ignore(record => record.Etc);

//
// See https://stackoverflow.com/a/38073718 for an IgnoreUnmappedProperties extension