我正在尝试从我的数据库中获取最新消息,但我一直收到此错误: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; }
}
}
答案 0 :(得分:0)
我通过确保最新消息实际上是列表而不是集合/可查询
来解决这个问题