[英]AutoMapper nulls out properties in destination object when the destination is an existing object with some populated properties
编辑:请不要介意。 正如我在下面所说的那样,它运行良好。 我的原始代码有一个巨大的错误,因为它比我的示例复杂得多。 谢谢你。
public class Full {
public string A {get;set;}
public string B {get;set;}
}
public class Half1 {
public string A {get;set;}
}
public class Half2{
public string B {get;set;}
}
CreateMap<Half1, Full>();
CreateMap<Half2, Full>();
var half1Items = //some data with A populated
var half2Items = //some data with B populated
//this will populate fullItems with A properties - so far so good
var fullItems = Mapper.Map<List<Half1>, List<Full>(half1Items);
//this will make all A properties null and populate all B properties - very sad :'(
fullItems = Mapper.Map(half2Items, fullItems);
最简单的解决方案是显式忽略该属性。
.ForMember(x => x.sdfsdf, opt => opt.Ignore())
你为什么要这样做? 因为理想情况下,您应该使用AssertConfigurationIsValid
验证您的映射, AssertConfigurationIsValid
这很烦人,但它AssertConfigurationIsValid
大量发布,以免发生奇怪和意外的破坏。
AutoMapper 检查以确保每个单个 Destination 类型成员在源类型上都有一个对应的类型成员。
或者,如果您感到幸运并且喜欢住在座位的边缘
.ForAllOtherMembers(opt => opt.Ignore());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.