[英]AutoMapper doesn't ignore nested types
I have a situation where AutoMapper doesn't work properly with ignoring members. 我遇到一种情况,即AutoMapper无法忽略成员而无法正常工作。 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>();
And when I map Class1 to Class1 当我将Class1映射到Class1时
Mapper.Map<Class1, Class1>(object1, object2);
In object 2 the class3List is empty, but before the mapping it had items. 在对象2中,class3List为空,但在映射之前它具有项。 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>();
It ignores the Class2 property as it should. 它会忽略Class2属性。 So how can I ignore class3List and not emptying it, when mapping Class1 to Class1?
那么,当将Class1映射到Class1时,如何忽略class3List而不是将其清空?
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?
克隆吗?
Looking at the API I think best to used UseDestinationValue() rather than Ignore. 查看API,我认为最好使用UseDestinationValue()而不是忽略。 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.