开发者

C# - Entity Framework - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

开发者 https://www.devze.com 2022-12-16 23:00 出处:网络
An unhandled exception of type \'System.StackOverflowException\' occurred in mscorlib.dll Make sure you do not have an infinite loop or infinite recursion.

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.G开发者_开发问答etRelatedReference<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. <grrrrr />

Chalk it up to a bad wizard error somewhere along the line.


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

Respository2 repo2 = new Repository2();

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.


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 think you need to set the Company -> Company relation to be lazy loaded.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号