EF框架CURD

  1. public partial class BaseDal<T>where T :class
  2. {
  3. //DbContext context = new HMOAContainer();
  4. DbContext context = ContextFactory.GetContext();
  5. //增加
  6. public int Add(T userInfo)
  7. {
  8. context.Set<T>().Add(userInfo);
  9. return context.SaveChanges();
  10. }
  11. //修改
  12. public int Edit(T userInfo)
  13. {
  14. context.Entry(userInfo).State = EntityState.Modified;
  15. return context.SaveChanges();
  16. }
  17. //删除
  18. public int Remove(int id)
  19. {
  20. T u1 = context.Set<T>().Find(id);
  21. context.Set<T>().Remove(u1);
  22. return context.SaveChanges();
  23. }
  24. public int Remove(int[] ids)
  25. {
  26. int counter = ids.Length;
  27. for (int i = 0; i < counter; i++)
  28. {
  29. T u1 = context.Set<T>().Find(ids[i]);
  30. context.Set<T>().Remove(u1);
  31. }
  32. return context.SaveChanges();
  33. }
  34. public int Remove(T userInfo)
  35. {
  36. context.Set<T>().Remove(userInfo);
  37. return context.SaveChanges();
  38. }
  39. //查询
  40. public T GetById(int id)
  41. {
  42. return context.Set<T>().Find(id);
  43. }
  44. public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda)
  45. {
  46. return context.Set<T>().Where(whereLambda);
  47. }
  48. public IQueryable<T> GetPageList<Tkey>(Expression<Func<T, bool>> whereLambds, Expression<Func<T, Tkey>> orderLambda, int pageIndex, int pageSize)
  49. {
  50. return context.Set<T>().Where(whereLambds)
  51. .OrderByDescending(orderLambda)
  52. .Skip((pageIndex - 1) * pageSize)
  53. .Take(pageSize);
  54. }
  55. }





posted @ 2016-09-16 21:32  大长腿的猪  阅读(565)  评论(2编辑  收藏  举报