EF5 操作数据库,分离数据操作与业务逻辑

来源:互联网 发布:seo营销是什么 编辑:程序博客网 时间:2024/05/16 07:56
  1. using Mvc.Entity;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data.Entity.Infrastructure;  
  5. using System.Linq;  
  6. using System.Linq.Expressions;  
  7. using System.Reflection;  
  8. using System.Text;  
  9. namespace Mvc.Dal  
  10. {  
  11.     /// <summary>  
  12.     /// 数据操作,泛型类  
  13.     /// </summary>  
  14.     /// <typeparam name="T">要操作的的表对应的实体类的类型</typeparam>  
  15.     public class DalGeneric<T> where T : class,new()  
  16.     {  
  17.         MyMvcCmsEntities db = new MyMvcCmsEntities();  
  18.  
  19.         #region 1.0 新增实体  +int Add(T model)  
  20.         public int Add(T model)  
  21.         {  
  22.             db.Set<T>().Add(model);  
  23.             return db.SaveChanges();//保存成功后,会将自增的id设置给 model的 主键属性,并返回受影响行数   
  24.         }  
  25.         #endregion  
  26.  
  27.         #region 2.0 根据id 删除实体  +int Del(T model)  
  28.         public int Del(T model)  
  29.         {  
  30.             db.Set<T>().Attach(model); db.Set<T>().Remove(model); return db.SaveChanges();  
  31.         }  
  32.         #endregion  
  33.  
  34.         #region 3.0 根据条件删除 +int DelBy(Expression<Func<T, bool>> delWhere)  
  35.         public int DelBy(Expression<Func<T, bool>> delWhere)  
  36.         {   //3.1查询要删除的数据  
  37.             List<T> listDeleting = db.Set<T>().Where(delWhere).ToList();  
  38.             //3.2将要删除的数据 用删除方法添加到 EF 容器中   
  39.             listDeleting.ForEach(u =>  
  40.             {  
  41.                 db.Set<T>().Attach(u);//先附加到 EF容器   
  42.                 db.Set<T>().Remove(u);//标识为 删除 状态   
  43.             });  
  44.             //3.3一次性 生成sql语句到数据库执行删除   
  45.             return db.SaveChanges();  
  46.         }  
  47.         #endregion  
  48.  
  49.         #region 4.0 修改 +int Modify(T model, params string[] proNames)  
  50.         /// <summary>  
  51.         /// 4.0 修改,如:  
  52.         /// T u = new T() { uId = 1, uLoginName = "asdfasdf" };  
  53.         /// this.Modify(u, "uLoginName");  
  54.         /// </summary>  
  55.         /// <param name="model"></param>  
  56.         /// <param name="proNames"></param>  
  57.         /// <returns></returns>  
  58.         public int Modify(T model, params string[] proNames)  
  59.         {  
  60.             //4.1将 对象 添加到 EF中  
  61.             DbEntityEntry entry = db.Entry<T>(model);  
  62.             //4.2先设置 对象的包装 状态为 Unchanged  
  63.             entry.State = System.Data.EntityState.Unchanged;  
  64.             //4.3循环 被修改的属性名 数组  
  65.             foreach (string proName in proNames)  
  66.             {  
  67.                 //4.4将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新   
  68.                 entry.Property(proName).IsModified = true;  
  69.             }  
  70.             //4.5一次性 生成sql语句到数据库执行  
  71.             return db.SaveChanges();  
  72.         }  
  73.         #endregion  
  74.  
  75.         #region 4.x 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)  
  76.         /// <summary>  
  77.         /// 4.0 批量修改  
  78.         /// </summary>  
  79.         /// <param name="model">要修改的实体对象</param>  
  80.         /// <param name="whereLambda">查询条件</param>  
  81.         /// <param name="modifiedProNames">要修改的 属性 名称</param>  
  82.         /// <returns></returns>  
  83.         public int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)  
  84.         {  
  85.             //4.1查询要修改的数据   
  86.             List<T> listModifing = db.Set<T>().Where(whereLambda).ToList();  
  87.             //获取 实体类 类型对象   
  88.             Type t = typeof(T); // model.GetType();  
  89.             //获取 实体类 所有的 公有属性   
  90.             List<PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();  
  91.             //创建 实体属性 字典集合   
  92.             Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>();  
  93.             //将 实体属性 中要修改的属性名 添加到 字典集合中 键:属性名 值:属性对象  
  94.             proInfos.ForEach(p => { if (modifiedProNames.Contains(p.Name)) { dictPros.Add(p.Name, p); } });  
  95.             //4.3循环 要修改的属性名  
  96.             foreach (string proName in modifiedProNames)  
  97.             {  
  98.                 //判断 要修改的属性名是否在 实体类的属性集合中存在  
  99.                 if (dictPros.ContainsKey(proName))  
  100.                 {  
  101.                     //如果存在,则取出要修改的 属性对象   
  102.                     PropertyInfo proInfo = dictPros[proName];  
  103.                     //取出 要修改的值   
  104.                     object newValue = proInfo.GetValue(model, null); //object newValue = model.uName;  
  105.                     //4.4批量设置 要修改 对象的 属性  
  106.                     foreach (T usrO in listModifing)  
  107.                     {  
  108.                         //为 要修改的对象 的 要修改的属性 设置新的值  
  109.                         proInfo.SetValue(usrO, newValue, null); //usrO.uName = newValue;  
  110.                     }  
  111.                 }  
  112.             }  
  113.             //4.4一次性 生成sql语句到数据库执行   
  114.             return db.SaveChanges();  
  115.         }  
  116.         #endregion  
  117.  
  118.         #region 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)  
  119.         /// <summary>  
  120.         /// 根据条件查询  
  121.         /// </summary>  
  122.         /// <param name="whereLambda"></param>  
  123.         /// <returns></returns>  
  124.         public List<T> GetListBy(Expression<Func<T, bool>> whereLambda)   
  125.         {   
  126.             return db.Set<T>().Where(whereLambda).ToList();   
  127.         }  
  128.         #endregion  
  129.  
  130.         #region 5.1 根据条件 排序 和查询 + List<T> GetListBy<TKey>  
  131.         /// <summary>  
  132.         /// 根据条件排序和查询  
  133.         /// </summary>  
  134.         /// <typeparam name="TKey"></typeparam>  
  135.         /// <param name="whereLambda"></param>  
  136.         /// <param name="orderLambda"></param>  
  137.         /// <returns></returns>  
  138.         public List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda)   
  139.         {   
  140.             return db.Set<T>().Where(whereLambda).OrderBy(orderLambda).ToList();   
  141.         }   
  142.         #endregion  
  143.  
  144.         #region 5.2 根据条件查询一个实体 +T GetBy(Expression<Func<T, bool>> whereLambda)  
  145.         /// <summary>  
  146.         ///   
  147.         /// </summary>  
  148.         /// <param name="whereLambda"></param>  
  149.         /// <returns></returns>  
  150.         public T GetBy(Expression<Func<T, bool>> whereLambda)  
  151.         {  
  152.             return db.Set<T>().Where(whereLambda).FirstOrDefault();   
  153.         }  
  154.         #endregion  
  155.  
  156.         #region 6.0 分页查询 + List<T> GetPagedList<TKey>  
  157.         /// <summary>  
  158.         /// 分页查询  
  159.         /// </summary>  
  160.         /// <typeparam name="TKey"></typeparam>  
  161.         /// <param name="pageIndex"></param>  
  162.         /// <param name="pageSize"></param>  
  163.         /// <param name="whereLambda"></param>  
  164.         /// <param name="orderBy"></param>  
  165.         /// <returns></returns>  
  166.         public List<T> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy)  
  167.         {  
  168.             // 分页 一定注意: Skip 之前一定要 OrderBy   
  169.             return db.Set<T>().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();  
  170.         }  
  171.         #endregion  
  172.     }  
  173. }  
0 0
原创粉丝点击