Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Hot to use IQueryable in WEB project - DB is always Disposes #1355


User avatar
0
klainer created

Hello, I´m using kendo grid, with ABP. I have services, which return IQueriable<T> to Web project. I need to use Kendo ToDataSourceResult extension method to filter data based on request params.

DataSourceResult result = gridViews.ToDataSourceResult(request);

But ABP always dispose DB context, so i got exception.

My example:

GridViewDto:

public class GridView : Entity, IMustHaveOrganizationUnit
    {
        public virtual long OrganizationUnitId { get; set; }

        public virtual string ViewName { get; set; }

        [Required]
        public virtual string Key { get; set; }  // název grigu

        [Required]
        public virtual string GridSettings { get; set; }

        [ForeignKey("User_Id")]
        public virtual User User { get; set; } 
        public long? User_Id { get; set; }
    }

    public class GridViewDto
    {
        public virtual string ViewName { get; set; }
        public virtual string Key { get; set; }  // název grigu
        public virtual string GridSettings { get; set; }
        public virtual string UserName { get; set; }
        public long? UserId { get; set; }
    }

SERVICE:

[UnitOfWork]
        public IQueryable<GridViewDto> GetUserGridViews(User user, string key)
        {
            if (user == null)            
                throw new ArgumentNullException("user");

            Mapper.Initialize(cfg =>
                cfg.CreateMap<GridView, GridViewDto>()
                    .ForMember(dto => dto.UserName, conf => conf.MapFrom(ol => ol.User.Name))
                );


            return _gridViewRepository.GetAll().Where(grid => grid.User_Id == user.Id && grid.Key == key).ProjectTo<GridViewDto>();
        }

CONTROLLER:

[DontWrapResult]
        [UnitOfWork]
        public async Task<ActionResult> GridViewDataRead([DataSourceRequest]DataSourceRequest request, string gridId)
        {
            var user = await _userManager.GetUserByIdAsync(AbpSession.UserId.Value);
            var gridViews = _gridViewService.GetUserGridViews(user, gridId);
            DataSourceResult result = gridViews.ToDataSourceResult(request);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed. 
at System.Data.Entity.Internal.InternalContext.CheckContextNotDisposed() 
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 
at System.Data.Entity.Internal.InternalContext.Initialize()

Thanks for help


1 Answer(s)
  • User Avatar
    0
    klainer created

    Solved by virtual keyword in action :) :D