首页 新闻 会员 周边 捐助

ef The connection was not closed. The connection's current state is connecti

0
悬赏园豆:50 [待解决问题]

namespace DataAccess.EFImp
{
public class EfRepository<T> : IDisposable, IRepository<T> where T : class, new()
{
private XXEntities context;
internal XXEntities Context
{
get { return context; }
}

private ObjectSet<T> entitySet;
internal ObjectSet<T> EntitySet
{
get { return entitySet; }
}
internal ObjectQuery<T> EntitySetQuery { get; set; }
internal ObjectQuery<T> EntitySetSingle { get; set; }

//bool isEnableLazyLoading
public EfRepository()
{
this.context = XXEntitiesManager.getInstance(false);

if (context == null)
throw new ArgumentNullException("context");

this.entitySet = context.CreateObjectSet<T>();
this.EntitySetQuery = this.entitySet;
this.EntitySetSingle = this.entitySet;
}

#region IRepository<T>

public T Update(T entity, Expression<Func<T, bool>> filter)
{
try
{
var entityFromDb = entitySet.Where(filter).FirstOrDefault();
if (entityFromDb != null)
entitySet.ApplyOriginalValues(entityFromDb);
entitySet.ApplyCurrentValues(entity);
context.SaveChanges();
return entity;
}
catch (Exception ex)
{
RefreshEntitySet();
throw ex;
}

}

public T Insert(T entity)
{
try
{
entitySet.AddObject(entity);
context.SaveChanges();
return entity;
}
catch(Exception ex)
{
RefreshEntitySet();
throw ex;
}
}

private void RefreshEntitySet()
{
XXEntitiesManager.webManager = null;
this.context = XXEntitiesManager.getInstance(false);
this.entitySet = context.CreateObjectSet<T>();
this.EntitySetQuery = this.entitySet;
this.EntitySetSingle = this.entitySet;

}
public void Delete(T entity)
{
try
{
entitySet.DeleteObject(entity);
context.SaveChanges();
}
catch (Exception ex)
{
RefreshEntitySet();
throw ex;
}
}
public void Delete(Expression<Func<T, bool>> filter)
{
try
{
ConditionBuilder Builder = new ConditionBuilder();
Builder.Build(filter.Body);
string sqlCondition = Builder.Condition;

var args = Builder.Arguments;
string commandText = string.Format("Update {0} Set {1} Where {2}", this.entitySet.EntitySet.Name, " IsActive=0 ", sqlCondition);
var Result = this.entitySet.Context.ExecuteStoreCommand(commandText, args);
}
catch (Exception ex)
{
RefreshEntitySet();
throw ex;
}
}
public IList<T> FindAll()
{
return entitySet.ToList();

}

public IQueryable<T> Query(Expression<Func<T, bool>> filter)
{
return EntitySetQuery.Where(filter);
}
public IList<T> QueryByPage<TKey>(Expression<Func<T, bool>> filter, Expression<Func<T, TKey>> orderBy, int orderType, int pageSize, int pageIndex, out int recordsCount)
{
recordsCount = Query(filter).Count();
return orderType == 0 ? Query(filter).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList() : Query(filter).OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
public IList<T> QueryByPage(Expression<Func<T, bool>> filter, string orderBy, int pageSize, int pageIndex, out int recordsCount)
{
recordsCount = Query(filter).Count();
return Query(filter).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
public virtual T GetSingleOrDefault(Expression<Func<T, bool>> filter)
{
return EntitySetSingle.Where(filter).SingleOrDefault();
}

#endregion

public void Dispose()
{
context.Dispose();
}
}
}

 

 

多人一起提交出现的问题 请大神忙帮看下

Elite_Y的主页 Elite_Y | 初学一级 | 园豆:117
提问于:2017-03-09 14:12
< >
分享
所有回答(1)
0

DbContext即用即放

dudu | 园豆:30948 (高人七级) | 2017-03-09 14:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册