“Dispose后访问DataContext”从控制器 ASP.NET MVC 3 中的 ActionResult 方法处理数据上下文时出错
这是我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不能确定,但这可能与
return View(menu.ToList())
有关,因为我相信实际结果(视图)直到稍后在 MVC 中才会执行管道,因此在释放WebDataContext
之前,不会执行ToList()
。我打赌这会解决这个问题: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 theToList()
isn't executed until after yourWebDataContext
is disposed. I bet this would resolve the issue:这就是我要做的,我将
WebDataContext
范围限制在Controller
级别,覆盖 Dispose()
并在其中调用 context.Dispose()反而。Here's what I would do, I would scope the
WebDataContext
at theController
level,override Dispose()
and call context.Dispose() in there instead.我很好奇您是否正在创建一个可枚举列表,然后可以稍后运行查询。
不知道你的“父母参考资料”是什么,很难说。但我 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)
将菜单传递到视图中实际上是进行浅复制(指针复制)。这将使连接保持活动状态。您需要执行菜单对象的深层复制。我敢打赌,如果您直接在数据上下文上调用 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.