Skip to main content

C# Automapper extension to ignore specified mapping properties of the domain class, not available on the model.

public static class AutomapperExtensions
{

    /// <summary>
    /// Automapper extension to ignore specified mapping 
    /// properties of the domain class, not available on the model.
    /// http://stackoverflow.com/a/16808867
    /// </summary>
    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);