0

I have an n-tier application ASP.NET MVC. I use entity framework in DAL. So i decided to use cache layer in dal for up app's performance. I've implement it but there is a problem. I put entity to memory cache first request after that when second request i try to get entity from cache it's throw object disposed error. I know this error normal because i can't it from cache. But i couldn't think it. What can i do? Should i use business object in dal or?

"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection"

    public IQueryable<Category> GetAll()
    {
        var cacheKey = string.Format("{0}_{1}", "CategoryRepository", "GetAll");

        var isExists = _cache.Contains(cacheKey) && Const.CacheIsActive;
        if (isExists)
        {
               //At here error: "The ObjectContext instance has been disposed and 
               //can no longer be used for operations that require a connection"
            return _cache.Get<IQueryable<Category>>(cacheKey);
        }
        else
        {
            var obj = _db.Categories;
            _cache.Add(cacheKey, obj);

            return obj;
        }
    }
Yargicx
  • 1,704
  • 3
  • 16
  • 36
  • 2
    `_db.Categories` is an `IQueryable`, and only contains instructions on how to get data (not store it). Try saving `_db.Categories.ToList()`? – Scott May 18 '18 at 15:32
  • Possible duplicate of [Differences between IQueryable, List, IEnumerator?](https://stackoverflow.com/questions/4844660/differences-between-iqueryable-list-ienumerator) – Scott May 18 '18 at 15:33
  • You are right. Thank you. – Yargicx May 18 '18 at 16:07

1 Answers1

0

in context write

protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            if (options != null)
            {
                options.UseMemoryCache(_cache);
            }
        }
Moaz Salem
  • 438
  • 3
  • 5