Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# 自动映射用子对象覆盖列表上缺少的源属性_C#_Automapper - Fatal编程技术网

C# 自动映射用子对象覆盖列表上缺少的源属性

C# 自动映射用子对象覆盖列表上缺少的源属性,c#,automapper,C#,Automapper,我对Automapper有问题。我设置了一个测试windows窗体应用程序,下面是代码。同时查看每个MessageBox后面的注释: public class FirstClass { public string FirstProp { get; set; } public IList<FirstClassChild> Children { get; set; } } public class FirstClassChild

我对Automapper有问题。我设置了一个测试windows窗体应用程序,下面是代码。同时查看每个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!!

        }
    }
公共类第一类
{
公共字符串FirstProp{get;set;}
公共IList子项{get;set;}
}
一等儿童
{
公共字符串FirstChildProp{get;set;}
}
公务舱二等舱
{
公共字符串FirstProp{get;set;}
公共字符串SecondProp{get;set;}
公共IList子项{get;set;}
}
公营二等儿童
{
公共字符串FirstChildProp{get;set;}
公共字符串SecondChildProp{get;set;}
}
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
AutoMapper.Mapper.CreateMap();
AutoMapper.Mapper.CreateMap();
var f=新的第一类{FirstProp=“FirstClass”};
f、 Children=新列表{new FirstClassChild{FirstChildProp=“FirstClass”};
var s=新的第二类{FirstProp=“SecondClass”,SecondProp=“SecondClass”};
s、 Children=新列表{new SecondClassChild{FirstChildProp=“SecondClass”,SecondChildProp=“SecondClass”};
自动映射器映射器映射(f,s);
var fc=new FirstClassChild{FirstChildProp=“FirstClass”};
var sc=newsecondclasschild{FirstChildProp=“SecondClass”,SecondChildProp=“SecondClass”};
自动映射器映射器映射(fc、sc);
MessageBox.Show(sc.FirstChildProp);//第一类
MessageBox.Show(sc.SecondChildProp);//第二类如预期
MessageBox.Show(s.FirstProp);//如预期的第一类
MessageBox.Show(s.SecondProp);//第二类按预期
MessageBox.Show(s.Children.First().FirstChildProp);//如预期的第一类
MessageBox.Show(s.Children.First().SecondChildProp);//不应为空!!
}
}
我能做些什么来避免这种情况?这是预期的行为吗?
无论如何,有谁能指导我如何使SecondClass childs SecondChildProp保持映射之前的“SecondClass”状态。

我问了一个类似的问题,发现了另一个类似的问题

我认为@PatrickSteele提出了一个很好的观点:当dest列表不一定与源列表有任何相似之处时,AutoMapper如何将源列表映射到现有对象的dest列表?i、 e.“

如果您确定
第一类
第二类
具有相同数量的
子级
,并且如果
第一类
的第n个子级始终对应于
第二类
的第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]);
        });

这是有道理的,谢谢。我没有意识到复杂性,被我的孤立案例蒙蔽了双眼。再想一想,关于AutoMapper不可能知道列表中的孩子的论点等等。AutoMapper映射FirstChildProp不是有点尴尬吗?对我来说似乎不一致。@John-在幕后,我不认为Automapper正在将FirstChildProp映射到现有的SecondClassChild对象。相反,我希望它会完全删除SecondClass的
子类
列表,并用它刚刚创建的SecondClassChild的新列表覆盖它(使用
FirstClass.Children
作为映射源)。当source.Children和dest.Children列表具有不同的长度时,这一点会变得更加明显。
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);
            }
        });