Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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_Adjacency List_Data Mapping - Fatal编程技术网

C# 自动映射邻接表模型的数据映射

C# 自动映射邻接表模型的数据映射,c#,automapper,adjacency-list,data-mapping,C#,Automapper,Adjacency List,Data Mapping,因此,我们有一个模型对象TreeNode: Public Class TreeNode{ Public int NodeId {get;set;} Public String Name {get;set;} Public int ParentId {get;set;} Public TreeNode Parent {get;set;} Public List<TreeNode> Children {get;set;} } 公共类树节点{ 公共int节点ID{ge

因此,我们有一个模型对象
TreeNode

Public Class TreeNode{
  Public int NodeId {get;set;}
  Public String Name {get;set;}
  Public int ParentId {get;set;}
  Public TreeNode Parent {get;set;}
  Public List<TreeNode> Children {get;set;}
}
公共类树节点{
公共int节点ID{get;set;}
公共字符串名称{get;set;}
Public int ParentId{get;set;}
公共树节点父节点{get;set;}
公共列表子项{get;set;}
}
此结构由使用的数据库提供支持。我正在使用WCF服务来填充我的模型类

我想这样做:

public static void ConfigureMappings()
{
  Mapper.CreateMap<TreeNodeDto, Taxonomy>()
  .AfterMap((s, d) =>
  {  
     //WCF service calls to get parent and children
     d.Children =  Mapper.Map<TreeNodeDto[], TreeNode[]>(client.GetTreeChildren(s)).ToList();
     d.Parent = Mapper.Map<TreeNodeDto, TreeNode>(client.GetTreeParent(s));
  });
}
publicstaticvoidconfiguremappings()
{
Mapper.CreateMap()
.AfterMap((s,d)=>
{  
//WCF服务调用以获取父项和子项
d、 Children=Mapper.Map(client.GetTreeChildren(s)).ToList();
d、 Parent=Mapper.Map(client.GetTreeParent);
});
}

但很明显,这会导致一个无限循环(如果我只将子对象映射到tho,它会起作用)。有没有办法使用AutoMapper填充我的树结构

我找到了这个部分解决方案。起初我以为这是我要找的,但经过进一步检查,只有从树的顶端开始,它才起作用。如果从中间开始,它不会填充父节点。

publicstaticvoidconfiguremappings()
{
Mapper.CreateMap()
.AfterMap((s,d)=>
{  
//WCF服务调用以获取父项和子项
d、 Children=Mapper.Map(client.GetTreeChildren(s)).ToList();
foreach(d.Children中的var child)
{
孩子。父母=d;
}
}

好吧。进一步检查后,我意识到只有从树的顶部开始,这个解决方案才有效。
public static void ConfigureMappings()
{
  Mapper.CreateMap<TreeNodeDto, Taxonomy>()
  .AfterMap((s, d) =>
  {  
     //WCF service calls to get parent and children
     d.Children =  Mapper.Map<TreeNodeDto[], TreeNode[]>(client.GetTreeChildren(s)).ToList();
    foreach( var child in d.Children)
    {
       child.Parent = d;
    }
}