首页 > 解决方案 > Automapper 如何将 IgnoreMap 属性与 MemberList.Source 选项一起使用

问题描述

如何将基于属性的成员忽略(例如[IgnoreMap])与MemberList.SourceAutomapper 9.0 中的选项结合使用?IgnoreMap 属性似乎被...忽略 - 以下示例抛出:

public class Source
{
    public string PropertyA { get; set; }
    [IgnoreMap]
    public string IgnoredProperty { get; set; }
}

public class Destination
{
    public string PropertyA { get; set; }

    public string PropertyC { get; set; }
}

public class MyProfile : Profile
{
    public MyProfile()
    {
        CreateMap<Source, Destination>(MemberList.Source);
    }
}

配置时使用MapperConfiguration.AssertConfigurationIsValid(). 它抛出 AssertConfigurationIsValid 就好像[IgnoreMap]不存在一样:

未映射的成员被发现。查看下面的类型和成员。添加自定义映射表达式、忽略、添加自定义解析器或修改源/目标类型对于没有匹配的构造函数,添加无参数 ctor、添加可选参数或映射所有构造函数参数 ======= ==================================================== ========= Source -> Destination(源成员列表) Mapping.Source -> Mapping.Destination(源成员列表)

未映射的属性:IgnoredProperty

我也尝试过[Ignore] [NotMapped]属性,但结果是一样的。

标签: c#automapper

解决方案


我没有尝试过自动映射器的属性忽略,我总是使用这个

CreateMap<Source, Destination>.ForMember(x => x.IgnoredProperty, opt => opt.Ignore());

或在这里尝试答案:如何配置 Automapper 以自动忽略具有 ReadOnly 属性的属性?


推荐阅读