0
.Skip()および.Take()linqメソッドをクエリに追加するとDBContextにエラーが発生する理由を理解するのに苦労しています。私は上手く動作するバージョンを持っていますが、それは上記の方法を持っておらず、違いを生むものをつかむことができません。ここで モデルバインディングページロード時にDBContextが処理される
は二つの方法、モデルバインドさGridViewのselectメソッドへの戻りデータです:実行するクエリを強制すべきである 私の知識で// 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を "using"ステートメントに入れる必要があります。ここで
は、例外の詳細は以下のとおりです。
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()呼び出しをシフトさせると、それを指摘していただきありがとうございます。 –