Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8273255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:24:42+00:00 2026-06-08T07:24:42+00:00

I am trying to get the Latest News from my database but I keep

  • 0

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; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:24:44+00:00Added an answer on June 8, 2026 at 7:24 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get the latest version of phpmailer from here: http://sourceforge.net/projects/phpmailer/files/
I have this query, and I am trying to get the latest comment for
I'm trying to use the Metakit library latest update, but I always get an
I'm trying to get the latest revision ID from my SVN project using Phing.
Im trying to use msbuild to get the latest version of my code from
I am just trying to get the latest auto generated key from the mysql
I am trying to get latest 10 posts from discussion board in sharepoint by
I'm trying to get the latest comment from an xml. An example can be
I am trying to get the latest NHibernate source from GitHub using the latest
I am trying to get the latest modified posts, from a group of posts,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.