Automapper v5 忽略未映射的属性
Automapper v5 Ignore unmapped properties
以前当我使用 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();
您可以使用 CreateMap
方法的 memberList
参数来指定您想要的验证。
CreateMap<TSource, TDestination>(MemberList.None)
MemberList.None
应该可以解决问题。您还可以在源验证或目标验证之间切换。
以前当我使用 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();
您可以使用 CreateMap
方法的 memberList
参数来指定您想要的验证。
CreateMap<TSource, TDestination>(MemberList.None)
MemberList.None
应该可以解决问题。您还可以在源验证或目标验证之间切换。