Base solution for your next web application
Open Closed

Passing DBContext or an IQueryable to a view? #2548


User avatar
0
rbarbosa created

Hi, would it be possible to pass the context without being disposed to a view?

Using DevExpress GridView and their sorting/grouping functionality depends on such IQueryable such example can be found here: <a class="postlink" href="https://www.devexpress.com/Support/Center/Example/Details/E3252">https://www.devexpress.com/Support/Cent ... ails/E3252</a>

on asp.net zero im using this:

public ActionResult AdvancedCustomBindingPartial()
        {
 return PartialView("LocationsTablePartial", _locationMasterRepository.GetAll());
}

I've also done a linq query from the service layer with almost the same results:

using (var unitOfWork = UnitOfWorkManager.Begin())
            {
                var query = _locationAppService.BuildLocationQuery();

                viewModel.ProcessCustomBinding(
                    query.GetDataRowCountAdvanced,
                    query.GetDataAdvanced,
                    query.GetSummaryValuesAdvanced,
                    query.GetGroupingInfoAdvanced,
                    query.GetUniqueHeaderFilterValuesAdvanced
                );
            unitOfWork.Complete();
            return PartialView("LocationsTablePartial", viewModel);
            }

and the exception in the view that follows: '((System.Data.Entity.DbSet<Abp.Location.LocationMaster>)Model).Local' threw an exception of type 'System.InvalidOperationException' "The operation cannot be completed because the DbContext has been disposed."

" at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\r\n at System.Data.Entity.Internal.Linq.InternalQuery1.GetEnumerator()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()\r\n at System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Linq.SystemCore_EnumerableDebugView1.get_Items()"

Any ideas?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you try to inject DbContext in your app service, query over it and return the resulted queryable to controller ?