Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当目标对象是具有某些已填充属性的现有对象时,AutoMapper会使目标对象中的属性为空_C#_Automapper - Fatal编程技术网

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
即将退出:)