我正在用實體框架構建mvc Web應用程序。查看Foreach循環:ObjectContext實例已被處置,不能再用於需要連接的操作
錯誤:
An exception of type 'System.ObjectDisposedException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
這是實體框架的一部分:
就像你看到的,我已經包括 「Alineas」,做.ToList()
public IList<Kop> Handle(RetrieveKoppenForDocumentQuery query)
{
using (var db = new BmDataContext())
{
var koppen = db.Kop.Where(s => s.Document.Id == query.Id)
.Include(s => s.TegelAfbeelding)
.Include(s => s.CollageAfbeeldingen)
.Include(s => s.FinancieleAfbeeldingen)
.Include(s => s.Alineas)
.ToList();
return _orderKoppenByIndex(koppen);
}
}
這是視圖KopListItems.cshtml:
@using PGE.Bestuursmonitor.Contracts.DataTypes
@model IList<Kop>
@* Helper for recursively rendering koppen*@
@helper SortableItem(Kop kop)
{
<div class="sortable-item" data-kopid="@kop.Id">
<div class="row">
<div class="col-md-7 title-column">
<i class="fa fa-arrows"></i> @kop.Titel
</div>
<div class="col-md-5">
<div class="col-md-3">
<span>
@kop.KopType
</span>
</div>
<div class="col-md-3">
<span>
@kop.Status
</span>
</div>
<div class="col-md-6">
<span class="pull-right">
@if (@kop.Alineas != null) // on this line I receive the exception
{
// here I would like to do some logic
}
</span>
</div>
</div>
</div>
<div class="sortable-container">
@foreach (var subKop in kop.Koppen)
{
@SortableItem(subKop);
}
</div>
</div>
}
@* Recursively render all kop items *@
<div id="koppen_sortable" class="sortable-container">
@foreach (Kop kop in Model)
{
@SortableItem(kop);
}
</div>
這是視圖KoppenList.cshtml:
@model PGE.Bestuursmonitor.ViewModels.Koppen.IKoppenListViewModel
<h1>@Model.DocumentTitel</h1>
@* Render kop list header *@
<div id="koppen_sortable_header">
<div class="row">
<div class="col-md-7"><strong>Titel</strong></div>
<div class="col-md-5">
<div class="col-md-3">
<strong>Type</strong>
</div>
<div class="col-md-3">
<strong>Status</strong>
</div>
<div class="col-md-6">
<span class="pull-right">
<button type="button" id="btn_add_sub" class="btn btn-success" title="Kop aanmaken" role="button" onclick="BM.Koppen.LoadAddKopView(null);">
<i class="fa fa-plus fa-lg"></i> Kop aanmaken
</button>
</span>
</div>
</div>
</div>
</div>
<div id="koppen_sortable_body">
@{ Html.RenderPartial("~/Views/Koppen/KopListItems.cshtml", @Model.Koppen); }
</div>
@* Store document id in html DOM, so javascript can reach it from multiple places *@
<input type="hidden" id="document_id" value="@Model.DocumentId" />
動作控制器:
[HttpGet]
public ActionResult KoppenList(string id)
{
ViewBag.PageId = id;
Document document = _retrieveStartPcDocumentQueryHandler.Handle(new RetrieveStartPcDocumentQuery());
RetrieveKoppenForDocumentQuery query = new RetrieveKoppenForDocumentQuery
{
Id = document.Id
};
IList<Kop> koppen = _retrieveKoppenForDocumentQueryHandler.Handle(query);
_koppenListViewModel.Koppen = koppen;
_koppenListViewModel.DocumentTitel = document.Titel;
_koppenListViewModel.DocumentId = document.Id;
return View("~/Views/Koppen/KoppenList.cshtml", _koppenListViewModel);
}
就像你在KopListItems.cshtml看到有2個foreach循環。外部的foreach循環工作正常,可以讀取「Alineas」。顯示子項目的內部foreach給出了這個奇怪的錯誤。出了什麼問題?我卡住了。
嘗試加入'.AsNoTracking()'來EF查詢 –
我嘗試,但得到了同樣的錯誤。 – user3585266