【发布时间】:2017-02-19 20:20:38
【问题描述】:
以前当我使用 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();
【问题讨论】: