0
我一直在努力理解爲什麼我添加一個.Skip()和.Take()linq方法查詢時,我得到一個DBContext處置錯誤。我有一個運行良好的版本,沒有上面提到的方法,只是無法掌握造成差異的原因。模型綁定DBContext部署在頁面加載
下面是兩種方法,即返回數據模型綁定GridView的選擇方法:
// This is the method that causes the DBContext disposed exception
public IQueryable<ProductCategory> GetCategoriesByPage(int pageNumber, int pageSize)
{
using (var dataModel = new MagicDayEntities())
{
var pagedCategories = (from categories in dataModel.ProductCategories
orderby categories.CategoryName ascending
select categories)
.Skip((pageSize - 1) * pageNumber)
.Take(pageSize);
pagedCategories.ToList().AsQueryable();
return pagedCategories;
}
}
//This method returns the data properly without error
public IQueryable<ProductCategory> GetAllCategories()
{
using (var dataModel = new MagicDayEntities())
{
var allCategories = (from categories in dataModel.ProductCategories
orderby categories.CategoryName ascending
select categories)
.ToList()
.AsQueryable();
return allCategories;
}
}
在我所知的ToList()應強制查詢運行,並克服了懶惰負荷的行爲,使DbContext處於「使用」語句中,這是一項要求。
以下是異常的詳細信息:
The operation cannot be completed because the DbContext has been disposed.
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.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.]
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +4249201
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +40
System.Linq.Queryable.Count(IQueryable`1 source) +196
System.Web.UI.WebControls.QueryableHelpers.CountHelper(IQueryable`1 queryable) +48
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +160
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +101
System.Web.UI.WebControls.ModelDataSourceView.ProcessSelectMethodResult(DataSourceSelectArguments arguments, DataSourceSelectResultProcessingOptions selectResultProcessingOptions, ModelDataMethodResult result) +249
System.Web.UI.WebControls.ModelDataSourceView.GetSelectMethodResult(DataSourceSelectArguments arguments) +92
System.Web.UI.WebControls.ModelDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +15
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +22
System.Web.UI.WebControls.ModelDataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +80
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.GridView.DataBind() +9
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75
System.Web.UI.Control.EnsureChildControls() +92
System.Web.UI.Control.PreRenderRecursiveInternal() +42
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Control.PreRenderRecursiveInternal() +160
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1586.0
有人能幫助我,並闡明此異常的原因一些輕? 在此先感謝。 彼得
確切地說,你已經在內存中強制數據,然後'IQueryable'沒有意義+1 –
感謝您指出,一旦我將.ToList()調用它的工作。 –