views:

1343

answers:

5

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?

A: 

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.

Shivkant
Hmmm. So you are suggesting I go into the wizard created code and stop it there? And what do I do the next time I build the project and EF rewrites the code for me?
Keith Barrows
@Shivkant, that's obvious and known. How do you propose a fix?
Alastair Pitts
A: 

I think you need to set the Company -> Company relation to be lazy loaded.

AZ
+1  A: 

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(); 
}
Florim Maxhuni
This looks like a possibility. Once I get some other fires put out I'll test this. Thanks.
Keith Barrows
it will most likely solve the recursing, but it shouldn't be recursing in the first place
Sander Rijken
A: 

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.

Keith Barrows
A: 

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.

Justin Soliz