操作无法完成,因为的DbContext已被释放 [英] The operation cannot be completed because the DbContext has been disposed
本文介绍了操作无法完成,因为的DbContext已被释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写在EF 4.1简单的应用程序将使用添加,删除,编辑和细节构成了我常用的数据源(数据库中心服务器)。
在我的控制器类我写:
I am writing a simple application in EF 4.1 which will use add, delete , edit and detail form my common data source (central server for database). In my Controller class i write:
public class RegController : Controller
{
//
// GET: /Reg/
private string CmdStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
public ActionResult Index()
{
using (var db = new RegModelContext(CmdStr))
{
return View(db.Registrant);
}
}
}
当我执行我的应用它给了我在索引视图错误在foreach语句:
when I am executing my Application it gave me an error in index view at foreach statement:
@model IEnumerable<Registration.Models.Register>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
UserName
</th>
<th>
Password
</th>
<th>
Email
</th>
<th>
Address
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
<td>
@item.UserName
</td>
<td>
@item.Password
</td>
<td>
@item.Email
</td>
<td>
@item.Address
</td>
</tr>
}
</table>
</body>
</html>
的错误是这样的:
因为的DbContext已被释放的操作无法完成。
The error is this: "The operation cannot be completed because the DbContext has been disposed."
推荐答案
您应该用一个列表来传递的模型
You should use a List to pass as your model
我认为db.Registrant返回用户列表?如果这样做这样的事情。
i assume that db.Registrant return a list of users?, if so do something like this
List<Registrant> items = null;
using (var db = new RegModelContext(CmdStr))
{
items = db.Registrant.ToList();
}
return View(items);
这篇关于操作无法完成,因为的DbContext已被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文