简体   繁体   English

C# - 实体框架 - mscorlib.dll中发生未处理的“System.StackOverflowException”类型异常

[英]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.

相关问题 mscorlib.dll中发生了未处理的“System.StackOverflowException”类型异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll 尝试从C#代码执行存储过程时,mscorlib.dll中发生了类型为&#39;System.StackOverflowException&#39;的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll while trying to execute stored procedure from c# code 发生mscorlib.dll错误时发生未处理的“System.StackOverflowException”类型异常? - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll error occurred? 调用递归函数期间,mscorlib.dll中发生了&#39;System.StackOverflowException&#39;类型的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll during calling recursive function 错误:mscorlib.dll中发生了&#39;System.StackOverflowException&#39;类型的未处理异常 - Error: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll RedisClientManager,mscorlib.dll中发生未处理的“System.StackOverflowException”类型异常 - RedisClientManager, An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll C#:&#39;System.StackOverflowException&#39;发生在mscorlib.dll中 - C#: 'System.StackOverflowException' occurred in mscorlib.dll 在启动我的MVC3应用程序时,在mscorlib.dll中出现“类型&#39;System.StackOverflowException&#39;的未处理异常”? - On starting my MVC3 application, getting “An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll”? WPF telerik TreeView控件中的mscorlib.dll发生类型为&#39;System.StackOverflowException&#39;的未处理异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in WPF telerik TreeView Control 局部视图:mscorlib.dll中发生了&#39;System.StackOverflowException&#39;类型的未处理异常 - Partial View: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM