Search code examples
asp.net-mvcobjectcontext

Latest News, The ObjectContext instance has been disposed


I am trying to get the Latest News from my database but I keep getting this error: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. the error happens on the NewsEntity.GetObject() method. I've tried adding the ToList, enabled LazyLoading, re-ordered the way I create the object sets. I have taken out the loading of the Author and Icon and that worked but I need them :) Thanks for any help.

Here is my NewsEntity class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Repository.Entity
{
    public class NewsEntity : BaseEntity<News>
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Content { get; set; }
        public int Icon { get; set; }
        public DateTime Posted { get; set; }
        public int Author { get; set; }
        public bool Deleted { get; set; }

        public virtual MemberEntity AuthorEntity { get; set; }
        public virtual IconEntity IconEntity { get; set; }

        public override News GetObject()
        {
            return new News
            {
                Id = Id,
                Title = Title,
                Summary = Summary,
                Content = Content,
                IconId = Icon,
                Icon = IconEntity.GetObject(),
                Posted = Posted,
                AuthorId = Author,
                Author = AuthorEntity.GetObject(),
                Deleted = Deleted
            };
        }
    }
}

This is my NewsObject class (For data transfer):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Repository.Entity
{
    public class News : BaseObject
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Content { get; set; }
        public int IconId { get; set; }
        public DateTime Posted { get; set; }
        public int AuthorId { get; set; }
        public bool Deleted { get; set; }

        public Member Author { get; set; }
        public Icon Icon { get; set; }
    }
}

This is my Database Context class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using Web.Repository.Entity;

namespace Web.Repository
{
    public class WebModelContext : ObjectContext
    {

        private IObjectSet<MemberEntity> _members;
        private IObjectSet<IconEntity> _icons;
        private IObjectSet<NewsEntity> _news;

        public WebModelContext()
            : base("name=WebRepository", "WebRepository")
        {
            ContextOptions.LazyLoadingEnabled = true;

            _members = CreateObjectSet<MemberEntity>();
            _icons = CreateObjectSet<IconEntity>();
            _news = CreateObjectSet<NewsEntity>();
        }

        public IObjectSet<MemberEntity> Members
        {
            get { return _members; }
        }

        public IObjectSet<IconEntity> Icons
        {
            get { return _icons; }
        }

        public IObjectSet<NewsEntity> News
        {
            get { return _news; }
        }
}

}

This is my NewsRepository class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Repository.Entity;

namespace Web.Repository
{
    public class NewsRepository : IDisposable
    {
        private WebModelContext _context;
        private WebModelContext Context
        {
            get
            {
                if (_context == null)
                    _context = new WebModelContext();
                return _context;
            }
        }

        public NewsRepository() { }

        public IEnumerable<News> GetLatestNews()
        {
            return Context.News.Where(news => !news.Deleted).OrderByDescending(news => news.Posted).Take(5).ToList().Select(news => news.GetObject());
        }

        #region Disposing
        private bool disposed;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing && _context != null)
                {
                    _context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

    }
}

This is my class to get the latest news:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Repository.Entity;
using Web.Repository;

namespace Web.Infrastructure
{
    public static class NewsHelper
    {
        public static IEnumerable<News> GetLatestNews()
        {
            IEnumerable<News> news;
            using (var repository = new NewsRepository())
            {
                news = repository.GetLatestNews();
            }
            return news;
        }
    }
}

This is my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Repository.Entity;
using Web.Models;
using Web.Infrastructure;

namespace Web.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            NewsListModel model = new NewsListModel { News = NewsHelper.GetLatestNews().ToList() };
            return View(model);
        }
    }
}

and finally this is my model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Repository.Entity;

namespace Web.Models
{
    public class NewsListModel
    {
        public IEnumerable<News> News { get; set; }
    }
}

Solution

  • I fixed this by ensuring the latest news was infact a list instead of a collection/iqueryable