Piszę prostą aplikację w EF 4.1, która będzie używać dodawania, usuwania, edycji i szczegółów z mojego wspólnego źródła danych (serwer centralny dla bazy danych). W mojej klasie Controller napisać:Operacji nie można ukończyć, ponieważ DbContext został usunięty
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);
}
}
}
kiedy jestem wykonanie Moja aplikacja dała mi błąd w widoku indeksu w 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>
Błąd to: „Operacja nie może być zakończone, ponieważ DbContext został usunięty. "
Musisz zwrócić db.Registrant.ToList(), ponieważ próbuje wykonać zapytanie po usunięciu datacontext, ToList() wymusi wcześniejsze wykonanie. – Giedrius