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

若如初见. 提交于 2019-11-29 02:59:22

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.

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 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!