C# 当目标对象是具有某些已填充属性的现有对象时,AutoMapper会使目标对象中的属性为空
编辑:请不要介意。正如我在下面所说的,它运行良好。我的原始代码中有一个巨大的错误,因为它比我的示例要复杂得多。多谢各位C# 当目标对象是具有某些已填充属性的现有对象时,AutoMapper会使目标对象中的属性为空,c#,automapper,C#,Automapper,编辑:请不要介意。正如我在下面所说的,它运行良好。我的原始代码中有一个巨大的错误,因为它比我的示例要复杂得多。多谢各位 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;} } Create
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);
公共类已满{
公共字符串A{get;set;}
公共字符串B{get;set;}
}
公共类半1{
公共字符串A{get;set;}
}
公共课半2{
公共字符串B{get;set;}
}
CreateMap();
CreateMap();
var half1Items=//某些数据已填充
var half2Items=//填充了B的某些数据
//这将用一个属性填充fullItems-到目前为止还不错
var fullItems=Mapper.Map最简单的解决方案是显式忽略该属性
.ForMember(x => x.sdfsdf, opt => opt.Ignore())
你为什么要这样做?因为理想情况下,您应该使用assertconfigurationsvalid
验证映射,尽管这很烦人,但它可以避免一大堆版本意外地被破坏
AutoMapper检查以确保每个目标类型
成员在源类型上具有相应的类型成员
或者如果你感到幸运,喜欢住在座位边上
.ForAllOtherMembers(opt => opt.Ignore());
.formMember(x=>x.sdfsdf,opt=>opt.Ignore())
?对于AllotherMembers
即将退出:)