本文介绍了Automapper覆盖与子对象列表中缺少源属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有Automapper问题。我建立一个测试Windows窗体应用程序及以下的代码。另外,也要看看每个消息框后的评论:I have a problem with Automapper. I set up a test windows form application and below is the code. Also look at the comments after each MessageBox:public class FirstClass { public string FirstProp { get; set; } public IList<FirstClassChild> Children { get; set; } } public class FirstClassChild { public string FirstChildProp { get; set; } } public class SecondClass { public string FirstProp { get; set; } public string SecondProp { get; set; } public IList<SecondClassChild> Children { get; set; } } public class SecondClassChild { public string FirstChildProp { get; set; } public string SecondChildProp { get; set; } } public partial class Form1 : Form { public Form1() { InitializeComponent(); AutoMapper.Mapper.CreateMap<FirstClass, SecondClass>(); AutoMapper.Mapper.CreateMap<FirstClassChild, SecondClassChild>(); var f = new FirstClass { FirstProp = "FirstClass" }; f.Children = new List<FirstClassChild> { new FirstClassChild { FirstChildProp = "FirstClass" } }; var s = new SecondClass { FirstProp = "SecondClass", SecondProp = "SecondClass" }; s.Children = new List<SecondClassChild> { new SecondClassChild { FirstChildProp = "SecondClass", SecondChildProp = "SecondClass" } }; AutoMapper.Mapper.Map(f, s); var fc = new FirstClassChild { FirstChildProp = "FirstClass" }; var sc = new SecondClassChild { FirstChildProp = "SecondClass", SecondChildProp = "SecondClass" }; AutoMapper.Mapper.Map(fc, sc); MessageBox.Show(sc.FirstChildProp);//FirstClass as expected MessageBox.Show(sc.SecondChildProp);//SecondClass as expected MessageBox.Show(s.FirstProp);//FirstClass as expected MessageBox.Show(s.SecondProp);//SecondClass as expected MessageBox.Show(s.Children.First().FirstChildProp);//FirstClass as expected MessageBox.Show(s.Children.First().SecondChildProp);//Empty not expected!! } } 我能做些什么来避免这种?这种行为是正常吗? 反正任何人都可以指导我如何做二等孩子的SecondChildProp保持二等,因为它是映射发生之前。What can I do to avoid this? Is this behavior expected?Anyway can anyone guide me how make SecondClass childs SecondChildProp to remain "SecondClass" as it is before the mapping occurs.推荐答案我这里问过类似的问题,发现另一个类似的有一个这里我觉得@PatrickSteele使一个很好的观点:AutoMapper应该怎样源列表映射到DEST列表现有对象,当DEST列表不一定承担任何相似之处源列表中?即但是,如果一个列表中有3个,其他列表中有5?I think @PatrickSteele makes a very good point: how is AutoMapper supposed to map a source list to a dest list of existing objects, when the dest list may not necessarily bear any resemblance to the source list? i.e. "But what if one list has 3 and the other list has 5?" 如果您确信的Firstclass 和二等有相同数量的孩子,和如果的的Firstclass 的第n个子总是对应于二等的第N个孩子,你可以尝试这样的事:If you are sure that FirstClass and SecondClass have the same number of Children, and if the FirstClass's Nth Child always corresponds to SecondClass's Nth child, you could try something like this:Mapper.CreateMap<FirstClass, SecondClass>() .ForMember(m => m.Children, o => o.Ignore()) .AfterMap((src, dest) => { for (var i = 0; i < dest.Children.Count; i++) Mapper.Map(src.Children[i], dest.Children[i]); });或 FirstChildProp 是某种独特键:Mapper.CreateMap<FirstClass, SecondClass>() .ForMember(m => m.Children, o => o.Ignore()) .AfterMap((src, dest) => { foreach (var dChild in dest.Children) { var sChild = src.Children.Single(c => c.FirstChildProp == dChild.FirstChildProp); Mapper.Map(sChild, dChild); } }); 这篇关于Automapper覆盖与子对象列表中缺少源属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-16 08:02