public partial class BaseDal<T>where T :class
{
//DbContext context = new HMOAContainer();
DbContext context = ContextFactory.GetContext();
//增加
public int Add(T userInfo)
{
context.Set<T>().Add(userInfo);
return context.SaveChanges();
}
//修改
public int Edit(T userInfo)
{
context.Entry(userInfo).State = EntityState.Modified;
return context.SaveChanges();
}
//删除
public int Remove(int id)
{
T u1 = context.Set<T>().Find(id);
context.Set<T>().Remove(u1);
return context.SaveChanges();
}
public int Remove(int[] ids)
{
int counter = ids.Length;
for (int i = 0; i < counter; i++)
{
T u1 = context.Set<T>().Find(ids[i]);
context.Set<T>().Remove(u1);
}
return context.SaveChanges();
}
public int Remove(T userInfo)
{
context.Set<T>().Remove(userInfo);
return context.SaveChanges();
}
//查询
public T GetById(int id)
{
return context.Set<T>().Find(id);
}
public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda)
{
return context.Set<T>().Where(whereLambda);
}
public IQueryable<T> GetPageList<Tkey>(Expression<Func<T, bool>> whereLambds, Expression<Func<T, Tkey>> orderLambda, int pageIndex, int pageSize)
{
return context.Set<T>().Where(whereLambds)
.OrderByDescending(orderLambda)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize);
}
}
原文:http://www.cnblogs.com/zkja/p/d14f03d3af49c262de6de1cec4b8effb.html