“DataContext accessed after Dispose” error when disposing datacontext from ActionResult method in controller ASP.NET MVC 3

我的未来我决定 提交于 2020-01-17 07:08:06

问题


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.


回答1:


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



回答2:


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




回答3:


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)




回答4:


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.



来源:https://stackoverflow.com/questions/8619483/datacontext-accessed-after-dispose-error-when-disposing-datacontext-from-actio

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