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

Kei*_*ows 23 c# stack-overflow recursion entity-framework

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();
}
Run Code Online (Sandbox Code Playgroud)

在返回时,它调用实体模型并尝试填充所有外键控对象(子对象).架构是[1公司有0到多个ProductsSold].出于某种原因,对以下代码的调用只会自行级联:

[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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,第一种方法调用第二种方法.第二种方法似乎无休止地称呼自己.

我如何解决这个问题?

Kei*_*ows 6

在从头开始删除和重建我的模型3次后,堆栈溢出神奇地消失了.<grrrr />

将它归结为沿线的某个错误的向导错误.