9

以前当我使用 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();
4

1 回答 1

12

您可以使用CreateMap方法的memberList参数来指定所需的验证。

CreateMap<TSource, TDestination>(MemberList.None)

应该做的MemberList.None伎俩。您还可以在源验证或目标验证之间切换。

Automapper - 选择要验证的成员

于 2016-11-23T05:46:52.240 回答