以前当我使用 Automapper v3.x 时,忽略未映射的属性可以通过简单地添加一个.IgnoreUnmappedProperties()
看起来像这样的扩展来完成
public static class AutoMapperExtensions
{
public static IMappingExpression<TSource, TDestination> IgnoreUnmappedProperties<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var typeMap = Mapper.FindTypeMapFor<TSource, TDestination>();
if (typeMap != null)
{
foreach (var unmappedPropertyName in typeMap.GetUnmappedPropertyNames())
{
expression.ForMember(unmappedPropertyName, opt => opt.Ignore());
}
}
return expression;
}
}
如何重写此扩展以与版本 5.x 一起使用。我当然可以将以下内容添加到每个属性中。
.ForMember(dest => dest.LastUpdatedBy, opt => opt.Ignore())
或者不打电话
Mapper.AssertConfigurationIsValid();