C# - 实体框架 - “System.StackOverflowException”类型的未处理异常发生在 mscorlib.dll 中
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 到多个已售产品]。由于某种原因,对以下代码的调用只是级联自身:
[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 中解决此问题?
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
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]. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
经过 3 次从头开始删除和重建模型后,堆栈溢出神奇地消失了。 <咕噜咕噜>>
将其归因于沿线某个地方的错误向导错误。
After 3 times at deleting and rebuilding my model from scratch, the stack overflow is magically gone. <grrrrr />
Chalk it up to a bad wizard error somewhere along the line.
我在使用 Asp.net Mvc、Sql Server 和 Linq to Entities 时遇到了同样的问题。通过调用堆栈,我发现我的两个存储库在另一个存储库中都有一个新的存储库调用。示例...
Repository1.cs
Repository2.cs
我猜我犯了一个愚蠢的错误,不完全确定发生了什么(也许有人可以在这里插话...),除了明显的,但我接受了存储库调用,一切正常现在就好了。
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...
Repository1.cs
Repository2.cs
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.
试试这个:
Try this:
我认为你需要设置公司->公司关系要延迟加载。
I think you need to set the Company -> Company relation to be lazy loaded.
StackOverflow 错误是由于无限循环使用完整的缓存内存而发生的,然后在最后显示堆栈溢出错误。
需要停止无限循环,并且调用同一函数不是最佳实践。
优化代码,尽量避免循环。
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.
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.