AutoMapper不忽略嵌套类型 [英] AutoMapper doesn't ignore nested types
问题描述
我有一个情况AutoMapper不能与忽视成员的工作。这里是阶级结构和映射。
I have a situation where AutoMapper doesn't work properly with ignoring members. Here is the class structure and mappings.
public class Class1 {
public Class2 Class2 { get; set; }
}
public class Class2 {
public List<Class3> class3List { get; set; }
}
Mapper.CreateMap<Class1, Class1>();
Mapper.CreateMap<Class2, Class2>
.ForMember(dest => dest.class3List, opt => opt.Ignore());
Mapper.CreateMap<Class3, Class3>();
当我1级映射到Class 1
And when I map Class1 to Class1
Mapper.Map<Class1, Class1>(object1, object2);
在对象2的class3List是空的,但映射收到的项目。如果我做了这样的映射。
In object 2 the class3List is empty, but before the mapping it had items. If I do the mapping like this.
Mapper.CreateMap<Class1, Class1>();
.ForMember(dest => dest.Class2, opt => opt.Ignore());
Mapper.CreateMap<Class2, Class2>();
Mapper.CreateMap<Class3, Class3>();
它忽略了等级2财产,因为它应该。 所以,我怎么可以忽略class3List并没有排空,当映射1级到1级?
It ignores the Class2 property as it should. So how can I ignore class3List and not emptying it, when mapping Class1 to Class1?
推荐答案
一般映射从一类中的一个类型来完成的一类其他类型。什么是你想在这里实现?克隆?
Usually mapping is done from a class of one type to a class of another type. What are you trying to achieve here? A Clone?
综观API,我认为最好是使用UseDestinationValue(),而不是忽略。不过,我觉得你的code测试,它没有似乎工作仍在。
Looking at the API I think best to used UseDestinationValue() rather than Ignore. I tested it with your code though and it didnt seem to work still.
Mapper.CreateMap<ParentFoo, ParentBar>()
.ForMember(b => b.Child, o => o.UseDestinationValue());
这篇关于AutoMapper不忽略嵌套类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!