“Dispose后访问DataContext”从控制器 ASP.NET MVC 3 中的 ActionResult 方法处理数据上下文时出错

发布于 2024-12-22 16:42:26 字数 879 浏览 2 评论 0原文

这是我的代码:

public ActionResult MainMenu(int id)
{
  using (WebDataContext context = new WebDataContext())
  {
    //var dataLoadOptions = new System.Data.Linq.DataLoadOptions();
    //dataLoadOptions.LoadWith<MenuCache>(x => x.Menu);
    //context.LoadOptions = dataLoadOptions;

    var menu = context.MenuCaches
                      .AsEnumerable()
                      .Where(x => x.ID == id 
                                  && (x.Local == true || x.National == true));
    foreach (var item in menu)
    {
      if (item.Parent.Parent != null && item.Parent.ParentID == 0)
      {
        menu = item.Children;
      }
    }
    return View(menu.ToList());
  }
}

我在网上找到了一些关于如何修复它的选项。一种是执行 yield return View(menu)); 但这给了我错误 ActionResult 不是迭代器。函数中的注释代码是我发现的另一个选项,但这也不起作用。有什么想法吗?多谢。

Here is my code:

public ActionResult MainMenu(int id)
{
  using (WebDataContext context = new WebDataContext())
  {
    //var dataLoadOptions = new System.Data.Linq.DataLoadOptions();
    //dataLoadOptions.LoadWith<MenuCache>(x => x.Menu);
    //context.LoadOptions = dataLoadOptions;

    var menu = context.MenuCaches
                      .AsEnumerable()
                      .Where(x => x.ID == id 
                                  && (x.Local == true || x.National == true));
    foreach (var item in menu)
    {
      if (item.Parent.Parent != null && item.Parent.ParentID == 0)
      {
        menu = item.Children;
      }
    }
    return View(menu.ToList());
  }
}

I found some options online on how to fix it. One was to do yield return View(menu)); but that gave me the error that ActionResult is not an iterator. The commented code in the function was another option that I found, but that didn't work either. Any ideas? Thanks a lot.

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

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

发布评论

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

评论(4

决绝 2024-12-29 16:42:26

我不能确定,但​​这可能与 return View(menu.ToList()) 有关,因为我相信实际结果(视图)直到稍后在 MVC 中才会执行管道,因此在释放 WebDataContext 之前,不会执行 ToList()。我打赌这会解决这个问题:

public ActionResult MainMenu(int id)
{
  IENumerable<MenuCache> menu;

  using (WebDataContext context = new WebDataContext())
  {
    menu = context.MenuCaches
                  .AsEnumerable()
                  .Where(x => x.ID == id 
                              && (x.Local == true || x.National == true));

    foreach (var item in menu)
    {
      if (item.Parent.Parent != null && item.Parent.ParentID == 0)
      {
        menu = item.Children;
      }
    }
    menu = menu.ToList();
  }
  return View(menu);
}

I can't be certain, but it's probably something to do with the return View(menu.ToList()) as I believe the actual result(the view) isn't executed until later in the MVC Pipe line, so the ToList() isn't executed until after your WebDataContext is disposed. I bet this would resolve the issue:

public ActionResult MainMenu(int id)
{
  IENumerable<MenuCache> menu;

  using (WebDataContext context = new WebDataContext())
  {
    menu = context.MenuCaches
                  .AsEnumerable()
                  .Where(x => x.ID == id 
                              && (x.Local == true || x.National == true));

    foreach (var item in menu)
    {
      if (item.Parent.Parent != null && item.Parent.ParentID == 0)
      {
        menu = item.Children;
      }
    }
    menu = menu.ToList();
  }
  return View(menu);
}
许久 2024-12-29 16:42:26

这就是我要做的,我将 WebDataContext 范围限制在 Controller 级别,覆盖 Dispose() 并在其中调用 context.Dispose()反而。

Here's what I would do, I would scope the WebDataContext at the Controller level, override Dispose() and call context.Dispose() in there instead.

聆听风音 2024-12-29 16:42:26

我很好奇您是否正在创建一个可枚举列表,然后可以稍后运行查询。
不知道你的“父母参考资料”是什么,很难说。但我 99.9% 确信这就是问题所在,您的 ToList 正在创建一个可以进一步查询的项目列表(并且在您的视图中)

ViewModel 肯定会摆脱这个问题,但请确保您的视图没有访问任何属性尚未加载到列表中每个项目的根级别(并且不会调用列表项目中的任何子项目)

Im curious if you are creating a list of enumerables, which can then run queries later.
Not knowing what your 'parent references are, its hard to say. but Im 99.9% sure thats the issue here, your ToList is creating a list of items that can further be queried (and are in your view)

A ViewModel would definitely get rid of this issue, but ensure your view isn't accessing any properties not yet loaded at the root level of each item in your list (and doesnt call into any child items in a list item)

逐鹿 2024-12-29 16:42:26

将菜单传递到视图中实际上是进行浅复制(指针复制)。这将使连接保持活动状态。您需要执行菜单对象的深层复制。我敢打赌,如果您直接在数据上下文上调用 dispose,视图将有一个空对象。

Passing menu into the view is actually taking a shallow copy (pointer copy). This will keep the connection alive. You need to perform a deep copy of the menu object. I would bet that if you directly called dispose on the data context the view would have a null object.

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