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();
}
}
}
多人一起提交出现的问题 请大神忙帮看下
DbContext即用即放