Automapper v5忽略未映射的属性 [英] Automapper v5 Ignore unmapped properties

查看:339
本文介绍了Automapper v5忽略未映射的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,当我使用Automapper v3.x时,可以通过简单地添加一个如下所示的.IgnoreUnmappedProperties()扩展名来忽略未映射的属性

Previously when I used Automapper v3.x ignoring unmapped properties could be done by simply adding a .IgnoreUnmappedProperties() extension which looked like this

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版一起使用.我当然可以在每个属性中添加以下内容.

How can this extension be rewritten to work with Version 5.x. I can of course add the following to each property.

.ForMember(dest => dest.LastUpdatedBy, opt => opt.Ignore())

还是不打电话

Mapper.AssertConfigurationIsValid();

推荐答案

您可以使用新的CreateMap方法参数来执行此操作,然后指定所需的验证.

You can do that using the new CreateMap method parameter, and specify the validation that you want.

CreateMap<TSource, TDestination>(MemberList.None)

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

The MemberList.None should do the trick. You can also switch between the source or destination validations.

自动映射器-选择要验证的成员

这篇关于Automapper v5忽略未映射的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆