【问题标题】:Automapper v5 Ignore unmapped propertiesAutomapper v5 忽略未映射的属性
【发布时间】: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();

【问题讨论】:

    标签: automapper automapper-5


    【解决方案1】:

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

    CreateMap<TSource, TDestination>(MemberList.None)
    

    MemberList.None 应该可以解决问题。您还可以在源验证或目标验证之间切换。

    Automapper - Selecting members to validate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-31
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多