如何结合Automapper 9.0中的[IgnoreMap]
选项使用基于属性的成员忽略(例如MemberList.Source
)? 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,添加可选参数或映射所有构造函数参数================================================== ===============源->目标(源成员列表)Mapping.Source->Mapping.Destination(来源成员列表)
未映射的属性:IgnoredProperty
我也尝试过[Ignore]
[NotMapped]
属性,但是结果是相同的。
我没有为自动映射器尝试过忽略属性,我总是使用此选项
CreateMap<Source, Destination>.ForMember(x => x.IgnoredProperty, opt => opt.Ignore());
或在这里尝试答案:How to configure Automapper to automatically ignore properties with ReadOnly attribute?