0

我正在尝试从我的数据库中获取最新消息,但我不断收到此错误:The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.错误发生在 NewsEntity.GetObject() 方法上。我尝试添加 ToList,启用 LazyLoading,重新排序我创建对象集的方式。我已经删除了作者和图标的加载并且有效,但我需要它们:) 感谢您的帮助。

这是我的 NewsEntity 类:

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
            };
        }
    }
}

这是我的 NewsObject 类(用于数据传输):

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; }
    }
}

这是我的数据库上下文类:

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; }
        }
}

}

这是我的 NewsRepository 类:

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

    }
}

这是我获取最新消息的班级:

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;
        }
    }
}

这是我的控制器:

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);
        }
    }
}

最后这是我的模型:

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; }
    }
}
4

1 回答 1

0

我通过确保最新消息实际上是列表而不是集合/可查询来解决此问题

于 2012-07-24T12:38:08.833 回答