C# - 实体框架 - mscorlib.dll 中发生类型为“System.StackOverflowException”的未处理异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2067866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Entity Framework - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
提问by Keith Barrows
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
Make sure you do not have an infinite loop or infinite recursion.
mscorlib.dll 中发生类型为“System.StackOverflowException”的未处理异常
请确保您没有无限循环或无限递归。
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]. For some reason, the call into the following code just cascades on itself:
在返回时,它调用实体模型并尝试填充所有外键对象(子对象)。架构是 [1 Company has 0 to many 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);
}
}
}
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?
我如何在 EF 中解决这个问题?
采纳答案by Keith Barrows
After 3 times at deleting and rebuilding my model from scratch, the stack overflow is magically gone. <grrrrr />
从头开始删除和重建模型 3 次后,堆栈溢出奇迹般地消失了。<grrrrr />
Chalk it up to a bad wizard error somewhere along the line.
将其归结为沿线某处的错误向导错误。
回答by Shivkant
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.
优化代码,尽量避免Looping。
回答by Adrian Zanescu
I think you need to set the Company -> Company relation to be lazy loaded.
我认为您需要将 Company -> Company 关系设置为延迟加载。
回答by Florim Maxhuni
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();
}
回答by Justin Soliz
I encountered this same exact issue using Asp.net Mvc, Sql Server, and 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. Example...
我在使用 Asp.net Mvc、Sql Server 和 Linq to Entities 时遇到了同样的问题。通过调用堆栈,我看到我的两个存储库在另一个存储库中每个都有一个新的存储库调用。例子...
Repository1.cs
存储库1.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.
我猜是我的一个愚蠢的错误,不完全确定发生了什么(也许有人可以在这里插话......)除了明显的问题,但我把存储库调用取出来,现在一切正常。