Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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
Asp.net mvc 自动映射相同类型的子映射_Asp.net Mvc_Entity Framework_Mapping_Automapper - Fatal编程技术网

Asp.net mvc 自动映射相同类型的子映射

Asp.net mvc 自动映射相同类型的子映射,asp.net-mvc,entity-framework,mapping,automapper,Asp.net Mvc,Entity Framework,Mapping,Automapper,我有第一个使用相同类型的子父级的代码 public class Comment { public int Id { get; set; } public string Text { get; set; } public int? ParentId { get; set; } public virtual Comment Parent { get; set; } public virtual IList<Comment> Child { g

我有第一个使用相同类型的子父级的代码

public class Comment
{
    public int Id { get; set; }

    public string Text { get; set; }

    public int? ParentId { get; set; }

    public virtual Comment Parent { get; set; }
    public virtual IList<Comment> Child { get; set; }
}
公共类注释
{
公共int Id{get;set;}
公共字符串文本{get;set;}
public int?ParentId{get;set;}
公共虚拟注释父项{get;set;}
公共虚拟IList子项{get;set;}
}
Fluent API:

modelBuilder.Entity<Comment>().Property(c => c.ParentId).IsOptional();
modelBuilder.Entity<Comment>().HasMany(c => c.Child).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
modelBuilder.Entity();
modelBuilder.Entity().HasMany(c=>c.Child).WithOptional(c=>c.Parent).HasForeignKey(c=>c.ParentId);
在实体框架中这很好。但当我尝试在Automapper上使用它时,我抛出了一个StackOverflowException

自动映射配置:

cfg.CreateMap<Comment, CommentDTO>().ForMember(d => d.Child, opt => opt.UseDestinationValue());
cfg.CreateMap();
评论:

public class CommentDTO
{
    public int Id { get; set; }

    public string Text { get; set; }

    public int? ParentId { get; set; }

    public virtual CommentDTO Parent { get; set; }
    public virtual IList<CommentDTO> Child { get; set; }
}
公共类注释到
{
公共int Id{get;set;}
公共字符串文本{get;set;}
public int?ParentId{get;set;}
对父项{get;set;}的公共虚拟注释
公共虚拟IList子项{get;set;}
}
控制器:

Context.Comments.GetAll().AsNoTracking().ProjectTo<CommentDTO>().AsQueryable();
Context.Comments.GetAll().AsNoTracking().ProjectTo().AsQueryable();

由于
Comment
CommentDTO
中的属性名称相同,您只需指示
AutoMapper
对它们进行映射,它就会为您完成以下操作:

Mapper.Initialize(x =>
        x.CreateMap<Comment, CommentDTO>().ReverseMap()
            .PreserveReferences()); 

最后一个注意事项是,在.NET命名约定中,如果缩写包含两个字符,例如
Input-Output>IO
,则建议对这两个字符都使用大写,例如
System.IO
。但如果大于2,如
数据传输对象>DTO
,则建议使用Pascal表示法。因此,您的类名应该是
CommentDto
而不是
CommentDto

谢谢您的帮助和有关命名约定的提示。不幸的是,在调用函数Context.Comments.ProjectTo()时,使用ReverseMap也会得到错误StackOverflowException;请尝试使用
.preserverences()
,因为我已经在我的答案中进行了编辑。对不起,没什么可做的。我认为StackOverflowException是由相同类型的子项列表引起的。这里的类似问题是,您需要设置MaxDepth,否则如果父/子关系的深度很深,您将得到StackOverflowException。看见
var commentDto = new CommentDTO { Child = new List<CommentDTO>(), Id = 1 };
var mapped = Mapper.Map<Comment>(commentDto);
var reverse = Mapper.Map<CommentDTO>(mapped);