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

c# entity-framework recursion stack-overflow

Question

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
Make sure you do not have an infinite loop or infinite recursion.

If this method is successful, the following code is executed:

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();
}

It calls the entity model on the response and attempts to fill any foreign keyed objects (child objects). [1 Company has 0 to Many ProductsSold] is the schema. The following code call simply keeps calling itself for some reason:

[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);
        }
    }
}

It is evident that the first method calls the second method. The second approach seems to keep repeating itself.

How do I make EF correct this?

1
23
2/22/2010 5:17:32 AM

Accepted Answer

The stack overflow miraculously disappears after deleting and recreating my model three times from scratch. Grrr, grrr,

Put it down to a terrible wizard mistake that occurred at some point.

6
2/1/2010 11:53:21 PM

Popular Answer

Using Asp.net Mvc, SQL Server, and Linq to Entities, I ran into the same same problem. I noticed that two of my repositories each had a new repository call in the other repository after looking through the callstack. Example...

Repository1.cs

Respository2 repo2 = new Repository2();

Repository2.cs

Repository1 repo1 = new Repository1();

It was probably a dumb error on my part; I'm not sure what's happening (maybe someone can weigh in here...) other from the obvious, but I removed the Repository calls, and now everything works as it should.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow