简体   繁体   English

Automapper使用子对象覆盖列表中缺少的源属性

[英]Automapper overwrites missing source property on list with child objects

I have a problem with Automapper. 我有Automapper的问题。 I set up a test windows form application and below is the code. 我设置了一个测试窗体应用程序,下面是代码。 Also look at the comments after each MessageBox: 另请查看每个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!!

        }
    }

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. 无论如何,任何人都可以指导我如何使SecondClass子孙SecondChildProp保持“SecondClass”,因为它是映射发生之前。

I asked a similar question here and found another similar one here . 在这里问了一个类似的问题,发现了另一个类似的问题

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? 我认为@PatrickSteele提出了一个非常好的观点:当dest列表可能不一定与源列表有任何相似之处时,AutoMapper如何将源列表映射到现有对象的dest列表? ie " But what if one list has 3 and the other list has 5? " 即“ 但如果一个列表有3个而另一个列表有5个怎么办?

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: 如果你确定FirstClassSecondClass具有相同数量的Children ,并且如果 FirstClass的第N个Child总是对应于SecondClass的第N个孩子,你可以尝试这样的事情:

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]);
        });

or if FirstChildProp is some kind of unique key: 或者如果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);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM