[英]C# - Entity Framework - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
mscorlib.dll中发生了未处理的“System.StackOverflowException”类型异常
Make sure you do not have an infinite loop or infinite recursion.确保没有无限循环或无限递归。
The below code is called on a success of this method: 以下代码在此方法成功时调用:
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();
}
On the return it calls into the Entity Model and tries to populate all foreign keyed objects (child objects). 在返回时,它调用实体模型并尝试填充所有外键控对象(子对象)。 The schema is [1 Company has 0 to many ProductsSold].
架构是[1公司有0到多个ProductsSold]。 For some reason, the call into the following code just cascades on itself:
出于某种原因,对以下代码的调用只会自行级联:
[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);
}
}
}
As you can see, the first method makes a call to the second method. 如您所见,第一种方法调用第二种方法。 The second method seems to call itself endlessly.
第二种方法似乎无休止地称呼自己。
How do I fix this in EF? 我如何解决这个问题?
After 3 times at deleting and rebuilding my model from scratch, the stack overflow is magically gone. 在从头开始删除和重建我的模型3次后,堆栈溢出神奇地消失了。 <grrrrr />
<grrrr />
Chalk it up to a bad wizard error somewhere along the line. 将它归结为沿线的某个错误的向导错误。
Try this: 尝试这个:
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();
}
I encountered this same exact issue using Asp.net Mvc, Sql Server, and Linq to Entities. 我使用Asp.net Mvc,Sql Server和Linq to Entities遇到了同样的问题。 Going through the callstack I saw that two of my repositories each had a new repository call in the other other repository.
通过callstack,我看到我的两个存储库在另一个存储库中都有一个新的存储库调用。 Example...
例...
Repository1.cs Repository1.cs
Respository2 repo2 = new Repository2();
Repository2.cs Repository2.cs
Repository1 repo1 = new Repository1();
I guess a silly mistake on my part, not exactly sure whats going on (maybe someone can chime in here...) aside from the obvious but I took the Repository calls out and everything works just fine now. 我想这是一个愚蠢的错误,不完全确定最新情况(也许有人可以在这里...)除了显而易见的但我把Repository调出来了,现在一切正常。
我认为您需要将公司 - >公司关系设置为延迟加载。
The StackOverflow error is occurring due to endless Looping which uses the full cache memory and then at the end it show the stack overflow error. StackOverflow错误是由于无限循环而发生的,循环使用完整的高速缓存,然后在结束时显示堆栈溢出错误。
The endless loop need to be stopped and it is not a best practice of calling the same function. 无限循环需要停止,并且调用相同的函数不是最佳实践。
Optimize the code and try to avoid the Looping. 优化代码并尽量避免循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.