我試過多次詢問此問題,現在或者沒有人知道答案,或者他們認爲之前已經回答過。我還沒有找到答案,所以我不認爲它有。獲取ObjectContext從會話變量訪問數據時處理的異常
使用MVC5 EF6。
我有一個搜索,我對存儲庫運行。在離開控制器之前,我放置搜索結果(使用ToList()來確保結果是枚舉的)。然後我返回傳遞結果的View。這工作正常。
[AllowAnonymous]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public async Task<ActionResult> SearchDispensaries(DispensarySearchModel model,
int pageNumber = 1, int pageSize = 10, string sortByField = "Dba", bool sortAscending = true) {
IEnumerable<Dispensary> found = null;
if ((model == null || !model.NewSearch) && Session["DispensariesFound"] != null) {
found = Session["DispensariesFound"] as List<Dispensary>;
}
if(ModelState.IsValid) {
found = await _dispensaries.SearchAsync(model, sortByField, sortAscending);
Session["DispensariesFound"] = found;
}
ViewBag.PageNumber = pageNumber;
ViewBag.PageSize = pageSize;
ViewBag.SortByField = sortByField;
ViewBag.SortAscending = sortAscending;
ViewBag.SortingEnabled = true;
if (Request.IsAjaxRequest()) {
return PartialView("_Dispensaries", found);
}
return View("DispensarySearchResults", found);
}
在視圖中,我有能力更改頁面或排序規則。我有另一個功能,這樣做...
[AllowAnonymous]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult SortAndPageDispensaries(int pageNumber = 1, int pageSize = 10, string sortByField = "Dba",
bool sortAscending = true) {
var found = (Session["DispensariesFound"] as IEnumerable<Dispensary>) ?? new List<Dispensary>();
found = _dispensaries.ApplySort(found, sortByField, sortAscending).ToList();
ViewBag.PageNumber = pageNumber;
ViewBag.PageSize = pageSize;
ViewBag.SortByField = sortByField;
ViewBag.SortAscending = sortAscending;
ViewBag.SortingEnabled = true;
if (Request.IsAjaxRequest()) {
return PartialView("_Dispensaries", found);
}
return View("DispensarySearchResults", found);
}
在返回行放置一個斷點我可以看到結果是枚舉。在這個函數中沒有包含或引用過DbContext。然而,在_Dispensaries查看我得到的錯誤
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
Source Error:
Line 38: <tr class="table-row">
Line 39: <td class="item-followers text-center">
Line 40: @item.FavoritedBy.Count
Line 41: </td>
Line 42: <td colspan="1" class="text-center">
Source File: d:\Projects\Web\Fhlora\Fhlora\Views\Shared\_Dispensaries.cshtml Line: 40
我有搜索其他實體類型幾個類似的網頁和該方法爲所有這些工作正常。有誰知道我可能會做錯什麼?我嘗試了我所知道的一切。
現在我才發現,原來當我在本地調試錯誤只發生,它是不會發生在發佈的網站上。 – 2014-11-14 19:15:42