C# 操作无法完成,因为 DbContext 已被释放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11859421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The operation cannot be completed because the DbContext has been disposed
提问by Fahad Mahmood
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:
我正在 EF 4.1 中编写一个简单的应用程序,它将使用 add、delete、edit 和 detail 表单我的公共数据源(数据库的中央服务器)。在我的控制器类中,我写道:
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);
}
}
}
when I am executing my Application it gave me an error in index view at foreach statement:
当我执行我的应用程序时,它在 foreach 语句的索引视图中给了我一个错误:
@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>
The error is this: "The operation cannot be completed because the DbContext has been disposed."
错误是这样的:“操作无法完成,因为 DbContext 已被释放。”
回答by JohnnBlade
You should use a List to pass as your model
您应该使用 List 作为模型传递
i assume that db.Registrant return a list of users?, if so do something like this
我假设 db.Registrant 返回用户列表?,如果是这样,请执行以下操作
List<Registrant> items = null;
using (var db = new RegModelContext(CmdStr))
{
items = db.Registrant.ToList();
}
return View(items);
回答by Brendan Vogt
Just to comment further, you need to separate your concerns. You shouldn't use the database context like that in a controller. Rather use it through a repository or service layer.
只是为了进一步评论,您需要分开您的关注点。您不应该像在控制器中那样使用数据库上下文。而是通过存储库或服务层使用它。
I also had this issue when using using. I removed the using part. Modify the code below to fit in with your scenario. Assuming you are to bring back a list of users. I would have this in my repository class:
我在使用的时候也有这个问题using。我删除了使用部分。修改下面的代码以适合您的场景。假设您要带回用户列表。我会在我的存储库类中有这个:
public class UserRepository : IUserRepository
{
MyDbContext dbContext = new MyDbContext();
public IEnumerable<User> GetAll()
{
return dbContext.Users;
}
}
This repository you then have injected in your controller by Autofac, Ninject, etc.
然后,您通过 Autofac、Ninject 等将这个存储库注入到您的控制器中。
In your controller it would look something like this:
在您的控制器中,它看起来像这样:
public class UserController : Controller
{
private readonly IUserRepository userRepository;
public UserController(IUserRepository userRepository)
{
this.userRepository = userRepository;
}
public ActionResult Index()
{
UserViewModel viewModel = new UserViewModel
{
Users = userRepository.GetAll()
};
}
}
And then in your view you can just loop through the users.
然后在您看来,您可以循环浏览用户。

