【问题标题】:Stack Overflow occurs when controller is accessed访问控制器时发生堆栈溢出
【发布时间】:2018-11-13 02:50:30
【问题描述】:
public ActionResult Index()
{
    var users = db.Users.Include(u => u.Users1).Include(u => u.User1);
    return View(users.ToList());
}

首先在 Visual Studio 中创建了一个 MVC / Entity 项目,DB。创建了两个控制器,一个命名为用户,另一个命名为任务。任务的页面视图工作正常,但每当我尝试访问用户视图页面时,我都会遇到此堆栈溢出。

Application Insights 遥测(未配置):{"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-06-03T17:26:09.6757730Z","tags":{"ai. cloud.roleInstance":"DESKTOP-2V3VGT6","ai.operation.id":"lH/a0kZkKsE=","ai.operation.name":"GET 任务/索引","ai.location.ip":" ::1","ai.internal.sdkVersion":"web:2.5.1-195","ai.internal.nodeName":"DESKTOP-2V3VGT6"},"data":{"baseType":"RequestData" ,"baseData":{"ver":2,"id":"|lH/a0kZkKsE=.82dfa1d2_","name":"GET 任务/索引","duration":"00:00:04.4166410","成功":true,"responseCode":"200","url":"http://localhost:62166/Tasks","properties":{"DeveloperMode":"true","_MS.ProcessedByMetricExtractors":"(Name:'Requests',版本:'1.0')"}}}} mscorlib.dll 中出现“System.StackOverflowException”类型的未处理异常

The program '[7192] iisexpress.exe' has exited with code -2147023895 (0x800703e9).

完整跟踪: https://pastebin.com/raw/846WTUqE

【问题讨论】:

  • 为什么你有两次相同的Include
  • 我贴的代码是MVC模板生成的。

标签: c# asp.net-mvc entity


【解决方案1】:

首先删除第二个包含语句,然后查看错误是什么。如果这不起作用,您可以尝试这样的 Select Where 表达式。将 ToList() 添加到变量而不是返回。移动 ToList() 不会有所不同,但更容易调试。

public ActionResult Index()
    {
        var users = db.Users.Select().Where(u => u.Users1).ToList();
        return View(users);
    }

【讨论】:

  • 这甚至不会编译,因为 Select 没有不带参数的重载,除非 Users1bool,否则 lambda 函数将不匹配所需的 Func<T, bool> Where.
猜你喜欢
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2012-01-12
相关资源
最近更新 更多