public class Repository<T> where T : class { public readonly DBContext _context; public readonly DbSet<T> _dbSet; public Repository() { //if (_context == null) //{ this._context = new DBContext(); //} this._dbSet = this._context.Set<T>(); } public T GetModel(Expression<Func<T, bool>> where) { //using (var __context = new UBIQ_CemeteryDBContext()) //{ // return __context.Set<T>().FirstOrDefault(where); return _dbSet.FirstOrDefault(where); //} }
以上代码写在 Repository 类中,BaseService 中如下定义:
private static Repository<NewInfo> ___Dal_NewInfo; /// <summary> /// NewInfo /// </summary> protected static Repository<NewInfo> __Dal_NewInfo { get { if (___Dal_NewInfo == null) { ___Dal_NewInfo = new Repository<NewInfo>(); } return ___Dal_NewInfo; } }
private static Repository<NewInfo> ___Dal_NewInfo; /// <summary> /// NewInfo /// </summary> protected static Repository<NewInfo> __Dal_NewInfo { get { if (___Dal_NewInfo == null) { ___Dal_NewInfo = new Repository<NewInfo>(); } return ___Dal_NewInfo; } }
业务层 返回 新闻详细页 ViewModel,代码如下:
public News_Details_Model GetModel_News_Details_Model(int id) { News_Details_Model vModel = new News_Details_Model(); if (id != 0) { string WebSiteStr = "文章中心"; NewInfo model = __Dal_NewInfo.GetModelByCache(n => n.NewID == id); vModel.NewInfor = model; //更新点击数 if (model != null) { #region 查找上一篇和下一篇文章 & 新闻标题 int PresentID = model.NewID; int CateID = model.CateID;// 类别ID if (PresentID > 0 && CateID > 0) { vModel.PrevModel = __Dal_NewInfo.GetModel(a => a.NewID < PresentID && a.CateID == CateID && a.IsOK == 1, x => x.OrderByDescending(n => n.NewID)); vModel.NextModel = __Dal_NewInfo.GetModel(a => a.NewID > PresentID && a.CateID == CateID && a.IsOK == 1, x => x.OrderBy(n => n.NewID)); var category = __Dal_NewCategory.GetModel(nc => nc.CateID == CateID); if (category != null) { WebSiteStr = category.CateName; } } #endregion #region 更新点击量 //更新点击量 model.NewClick += 1; __Dal_NewInfo.Update(model, true); #endregion } vModel.WebSiteStr = WebSiteStr; } return vModel; }
以上是全部代码,这样使用的话,本机一个人正常访问没有问题,但是若快速使用浏览器快捷键新窗口打开新闻详细,则会出现问题,如下图:
请问一下,,你解决这个问题了吗?