I am new to Design Patterns and am working on a project based on the Patterns In Action 4.0 solution. I am trying to implement paging support in an MVC web application and am having an issue. The paging library I am using can be found in Stephen Walthers' MVC Unleashed book. The library makes use of Linq Extensions to add the paging logic to the Entity framework query.
The issue I am having is when the Paging Linq Extension tries to count the number of records. I get an error message that states "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection". I understand that the connection is closed since in the repository class the method utilizes a "using" statement. After the query is complete the connection is closed. What would be the best way to add paging support to the repository pattern?
Paging code below:
public static class PagingLinqExtensions
public static PagedList<T> ToPagedList<T>(this IQueryable<T> allItems, int? pageIndex, int pageSize)
return ToPagedList<T>(allItems, pageIndex, pageSize, string.Empty);
public static PagedList<T> ToPagedList<T>(this IQueryable<T> allItems, int? pageIndex, int pageSize, string sort)
var truePageIndex = pageIndex ?? 0;
var itemIndex = truePageIndex * pageSize;
var pageOfItems = allItems.Skip(itemIndex).Take(pageSize);
var totalItemCount = allItems.Count();
return new PagedList<T>(pageOfItems, truePageIndex, pageSize, totalItemCount, sort);
Repository code below:
public IQueryable<Asset> GetAll()
using (var context = DataObjectFactory.CreateContext())
return context.AssetEntities.Select(x => AssetMapper.ToBusinessObject(x));