mscorlib.dll中发生未处理的“System.StackOverflowException”类型异常
确保没有无限循环或无限递归。
以下代码在此方法成功时调用:
internal static List<RivWorks.Model.Negotiation.ProductsSold> GetProductsSoldByCompany(Guid CompanyID)
{
var ret = from a in _dbRiv.ProductsSold where a.Company.CompanyId == CompanyID select a;
return ret.ToList();
}
在返回时,它调用实体模型并尝试填充所有外键控对象(子对象)。架构是[1公司有0到多个ProductsSold]。出于某种原因,对以下代码的调用只是级联:
[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("RIV_Model", "FK_ProductsSold_Company", "Company")]
[global::System.Xml.Serialization.XmlIgnoreAttribute()]
[global::System.Xml.Serialization.SoapIgnoreAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public Company Company
{
get
{
return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Company>("RIV_Model.FK_ProductsSold_Company", "Company").Value;
}
set
{
((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Company>("RIV_Model.FK_ProductsSold_Company", "Company").Value = value;
}
}
/// <summary>
/// There are no comments for Company in the schema.
/// </summary>
[global::System.ComponentModel.BrowsableAttribute(false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public global::System.Data.Objects.DataClasses.EntityReference<Company> CompanyReference
{
get
{
return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Company>("RIV_Model.FK_ProductsSold_Company", "Company");
}
set
{
if ((value != null))
{
((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference<Company>("RIV_Model.FK_ProductsSold_Company", "Company", value);
}
}
}
如您所见,第一种方法调用第二种方法。第二种方法似乎无休止地称呼它。
如何在EF中解决此问题?
答案 0 :(得分:6)
从头开始删除并重建我的模型3次后,堆栈溢出神奇地消失了。 &lt; grrrrr /&gt;
将它归结为某个地方出现的错误向导错误。
答案 1 :(得分:1)
试试这个:
internal static List<RivWorks.Model.Negotiation.ProductsSold> GetProductsSoldByCompany(Guid CompanyID)
{
var ret = from a in _dbRiv.Company where a.CompanyId == CompanyID select a.ProductsSolds;
return ret.ToList();
}
答案 2 :(得分:1)
我使用Asp.net Mvc,Sql Server和Linq to Entities遇到了同样的问题。通过callstack,我看到我的两个存储库在另一个存储库中都有一个新的存储库调用。实施例...
Repository1.cs
Respository2 repo2 = new Repository2();
Repository2.cs
Repository1 repo1 = new Repository1();
我认为这是一个愚蠢的错误,并不完全确定最新情况(也许有人可以在这里...)除了显而易见的但我拿出了Repository调用,现在一切正常。
答案 3 :(得分:0)
我认为您需要设置公司 - &gt;公司关系是懒惰加载。
答案 4 :(得分:-6)
StackOverflow错误是由于无限循环而发生的,它使用完整的高速缓存,然后在结束时显示堆栈溢出错误。
需要停止无限循环,这不是调用相同函数的最佳实践。
优化代码并尽量避免循环。