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

发布于 2024-08-17 09:02:25 字数 2143 浏览 4 评论 0原文

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

返回时,它调用实体模型并尝试填充所有外键对象(子对象)。架构为 [1 公司有 0 到多个已售产品]。由于某种原因,对以下代码的调用只是级联自身:

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

如您所见,第一个方法调用了第二个方法。第二种方法似乎无休止地调用自己。

如何在 EF 中解决此问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

燃情 2024-08-24 09:02:25

经过 3 次从头开始删除和重建模型后,堆栈溢出神奇地消失了。 <咕噜咕噜>>

将其归因于沿线某个地方的错误向导错误。

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.

世态炎凉 2024-08-24 09:02:25

我在使用 Asp.net Mvc、Sql Server 和 Linq to Entities 时遇到了同样的问题。通过调用堆栈,我发现我的两个存储库在另一个存储库中都有一个新的存储库调用。示例...

Repository1.cs

Respository2 repo2 = new Repository2();

Repository2.cs

Repository1 repo1 = new Repository1();

我猜我犯了一个愚蠢的错误,不完全确定发生了什么(也许有人可以在这里插话...),除了明显的,但我接受了存储库调用,一切正常现在就好了。

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.

無處可尋 2024-08-24 09:02:25

试试这个:

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

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(); 
}
痴者 2024-08-24 09:02:25

我认为你需要设置公司->公司关系要延迟加载。

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

对你再特殊 2024-08-24 09:02:25

StackOverflow 错误是由于无限循环使用完整的缓存内存而发生的,然后在最后显示堆栈溢出错误。

需要停止无限循环,并且调用同一函数不是最佳实践。

优化代码,尽量避免循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文