Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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#_Entity Framework_Automapper_Automapper 6 - Fatal编程技术网

C# 使用映射导致堆栈溢出的自引用循环

C# 使用映射导致堆栈溢出的自引用循环,c#,entity-framework,automapper,automapper-6,C#,Entity Framework,Automapper,Automapper 6,根据文档,应尽可能为AutoMapper自动设置参考 从6.1.0开始,在配置中自动设置PreserveReferences 只要有可能,时间就是 我也尝试过将MaxDepth设置为1,但是我仍然得到一个堆栈溢出异常,映射如下。我是否可以绕过这个问题,或者我是否需要修改视图模型 cfg.CreateMap<ArticleViewModel, Article>(MemberList.Source) .MaxDepth(1) .EqualityComparison((s

根据文档,应尽可能为AutoMapper自动设置参考

从6.1.0开始,在配置中自动设置PreserveReferences 只要有可能,时间就是

我也尝试过将MaxDepth设置为1,但是我仍然得到一个堆栈溢出异常,映射如下。我是否可以绕过这个问题,或者我是否需要修改视图模型

cfg.CreateMap<ArticleViewModel, Article>(MemberList.Source)
    .MaxDepth(1)
    .EqualityComparison((src, dst) => src.Id == dst.Id);
cfg.CreateMap(MemberList.Source)
.MaxDepth(1)
.EqualityComparison((src,dst)=>src.Id==dst.Id);
导致堆栈溢出异常的代码:

var article = await iArticleRepository.GetAsync(id);
//The mapping below causes the exception
var mappedArticle = Mapper.Map<ArticleViewModel>(article);
var article=await iArticleRepository.GetAsync(id);
//下面的映射导致异常
var mappedArticle=Mapper.Map(article);
实体:

public class Article: IEntity<int>
{
    [Key]
    public int Id { get; set; }

    ...

    public int SupplierId { get; set; }

    public virtual Supplier Supplier { get; set; }
}

public class Supplier: IEntity<int>
{
    [Key]
    public int Id { get; set; }

    ...

    public virtual ICollection<Contact> Contacts { get; set; }
}

public class Contact: IEntity<int>
{
    [Key]
    public int Id { get; set; }

    ...
    public virtual ICollection<Supplier> Suppliers { get; set; }
}
公共类文章:IEntity
{
[关键]
公共int Id{get;set;}
...
public int SupplierId{get;set;}
公共虚拟供应商{get;set;}
}
公共类供应商:IEntity
{
[关键]
公共int Id{get;set;}
...
公共虚拟ICollection联系人{get;set;}
}
公共类联系人:IEntity
{
[关键]
公共int Id{get;set;}
...
公共虚拟ICollection供应商{get;set;}
}
查看模型:

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

    ...
    public SupplierViewModel Supplier { get; set; }

}

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

    ...
    public List<ContactViewModel> Contacts { get; set; }

}

public class ContactViewModel
{
    public int Id { get; set; }
    ... 
    public List<SupplierViewModel> Suppliers { get; set; }
}
公共类ArticleViewModel
{
公共int Id{get;set;}
...
公共供应商视图模型供应商{get;set;}
}
公共类供应商视图模型
{
公共int Id{get;set;}
...
公共列表联系人{get;set;}
}
公共类ContactViewModel
{
公共int Id{get;set;}
... 
公共列表供应商{get;set;}
}

好吧,不清楚什么时候可能意味着什么。因为之前的文件表明

事实证明,这种跟踪非常昂贵,您需要选择使用PreserveReferences来实现圆形贴图

看起来您的方案失败,属于不可能的类别:)

让我们不要依赖于此,而使用显式的选择加入。此示例模型中的循环引用介于
供应商
联系人
之间,因此您必须在其中一个涉及的类映射中指定,例如:

cfg.CreateMap<ArticleViewModel, Article>(MemberList.Source)
    .MaxDepth(1)
    .EqualityComparison((src, dst) => src.Id == dst.Id);

cfg.CreateMap<SupplierViewModel, Supplier>(MemberList.Source)
    .PreserveReferences()
    .EqualityComparison((src, dst) => src.Id == dst.Id);
cfg.CreateMap(MemberList.Source)
.MaxDepth(1)
.EqualityComparison((src,dst)=>src.Id==dst.Id);
CreateMap(MemberList.Source)
.参考资料()
.EqualityComparison((src,dst)=>src.Id==dst.Id);

这应该在下一个MyGet版本中修复。

您的联系人实体有一组其他联系人,而您的ContactViewModel有一组SupplierViewModels。是这样吗?@Fabiano感谢您的关注,当然不应该是供应商。现在已更新。希望对于拥有数百个CreateMap的用户(如我们),可以使用一行代码为所有地图添加PreserveReferences:cfg.ForAllMaps((typeMap,mappingExpression)=>mappingExpression.PreserveReferences());不幸的是,这样做不会带来性能改进的好处。谢谢。我以前试过这个,但是没有MaxDepth。当使用这两种方法时,它仍然有效!将更新您的答案并标记为已接受。