Entity Framework底层操作封装(1)

最近做移动的项目,要求底层数据库是Oracle,但是因为本机是Sqlserver的环境。想了下,于是使用Entity Framework 进行开发,在开发完成以后切换到Oracle环境。想了下,就决定把以前封装的Linq的底层操作类进行了修改,形成EF的底层操作类。这样在自己熟悉的环境下,效率也会高很多,及时以后这个不好用,我切换起来也会简单很多。多的不过了,各位看官上代码,首先上的第一个类是底层操作的类。

 

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Linq.Expressions;  
  6. using System.Data.Entity;  
  7. using NOAS.PublicOpinionMonitor.ENTITY;  
  8. using System.Data.Linq;  
  9. using System.Data.Objects;  
  10. using System.Reflection;  
  11. using System.Threading;  
  12.   
  13. namespace NOAS.PublicOpinionMonitor.Access.Common  
  14. {  
  15.     public class DataCommon  
  16.     {  
  17.         static readonly string Connection = "name=POMonitorDBEntities";  
  18.         POMonitorDBEntities entities = new ENTITY.POMonitorDBEntities(Connection);  
  19.  
  20.         #region 根据SQL语句查询数据  
  21.         public IEnumerable<T> ExecuteQuery<T>(string conn, string query, params object[] parms)  
  22.         {  
  23.             try  
  24.             {  
  25.                 if (string.IsNullOrEmpty(conn))  
  26.                 {  
  27.                     return entities.ExecuteStoreQuery<T>(query, parms);  
  28.                 }  
  29.                 else  
  30.                 {  
  31.                     DataContext myDC = new DataContext(conn);  
  32.                     return myDC.ExecuteQuery<T>(query, parms);  
  33.                 }  
  34.             }  
  35.             catch (Exception ex)   
  36.             {  
  37.                 throw new Exception(ex.Message);  
  38.             }  
  39.         }  
  40.         public IEnumerable<T> ExecuteQuery<T>(string query)  
  41.         {  
  42.             return ExecuteQuery<T>(string.Empty, query, new object[] { });  
  43.         }  
  44.         #endregion  
  45.  
  46.         #region 执行操作类型SQl语句  
  47.         /// <summary>  
  48.         /// 执行SQL命令  
  49.         /// </summary>  
  50.         /// <param name="sqlCommand"></param>  
  51.         /// <returns></returns>  
  52.         public int ExecuteSqlCommand(string sqlCommand, string connection = null)  
  53.         {  
  54.             if (string.IsNullOrEmpty(connection))  
  55.                 return entities.ExecuteStoreCommand(sqlCommand); //ExecuteCommand(sqlCommand);  
  56.             else  
  57.             {  
  58.                 POMonitorDBEntities entitiesNew = new POMonitorDBEntities(connection);  
  59.                 return entitiesNew.ExecuteStoreCommand(sqlCommand);  
  60.             }  
  61.         }  
  62.         /// <summary>  
  63.         /// 执行SQL命令  
  64.         /// </summary>  
  65.         /// <param name="sqlCommand"></param>  
  66.         /// <returns></returns>  
  67.         public void ExecuteSqlCommand(string sqlCommand)  
  68.         {  
  69.             ExecuteSqlCommand(connection: string.Empty, sqlCommand: sqlCommand);  
  70.         }  
  71.         #endregion  
  72.  
  73.  
  74.         #region 私有方法  
  75.         private ObjectSet<T> GetTable<T>() where T : class  
  76.         {  
  77.             try  
  78.             {  
  79.                 ObjectSet<T> customers = entities.CreateObjectSet<T>();  
  80.                 return customers;  
  81.             }  
  82.             catch (Exception ex)  
  83.             {  
  84.                 throw ex;  
  85.             }  
  86.   
  87.         }  
  88.         #endregion  
  89.  
  90.         #region 统计指定条件的数据量  
  91.         /// <summary>  
  92.         /// 统计指定条件的数据量  
  93.         /// </summary>  
  94.         /// <typeparam name="T"></typeparam>  
  95.         /// <param name="query"></param>  
  96.         /// <returns></returns>  
  97.         public virtual int Count<T>(Expression<Func<T, bool>> query) where T : class  
  98.         {  
  99.             var table = GetTable<T>();  
  100.             return (from t in table  
  101.                     select t).Where(query).Count();  
  102.         }  
  103.         #endregion  
  104.  
  105.  
  106.         #region 获取单个实体  
  107.         /// <summary>  
  108.         /// 获取单个实体  
  109.         /// </summary>  
  110.         /// <typeparam name="T">泛型类型参数</typeparam>  
  111.         /// <param name="express">查询条件</param>  
  112.         /// <returns></returns>  
  113.         public virtual T GetSingleEntity<T>(Expression<Func<T, bool>> query) where T : class  
  114.         {  
  115.             try  
  116.             {  
  117.                 var table = GetTable<T>();  
  118.                 return (from t in table  
  119.                         select t).Where(query).SingleOrDefault();  
  120.             }  
  121.             catch (Exception ex)  
  122.             {  
  123.                 throw ex;  
  124.             }  
  125.         }  
  126.         #endregion  
  127.  
  128.  
  129.         #region 更新实体  
  130.         /// <summary>  
  131.         /// 更新实体  
  132.         /// </summary>  
  133.         /// <typeparam name="T">泛型类型参数</typeparam>  
  134.         /// <param name="entity">更改后的实体</param>  
  135.         public virtual bool Update<T>(T entity) where T : class  
  136.         {  
  137.             object propertyValue = null;  
  138.             T entityFromDB = GetSingleEntity<T>(s => s == entity);  
  139.             if (null == entityFromDB)  
  140.                 return false;  
  141.             PropertyInfo[] properties = entityFromDB.GetType().GetProperties();  
  142.             foreach (PropertyInfo property in properties)  
  143.             {  
  144.                 propertyValue = null;  
  145.                 if (null != property.GetSetMethod())  
  146.                 {  
  147.                     PropertyInfo entityProperty =  
  148.                           entity.GetType().GetProperty(property.Name);  
  149.                     if (entityProperty.PropertyType.BaseType ==  
  150.                         Type.GetType("System.ValueType") ||  
  151.                         entityProperty.PropertyType ==  
  152.                         Type.GetType("System.String"))  
  153.   
  154.                         propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);  
  155.                     if (propertyValue == null)  
  156.                     {  
  157.                         Thread.Sleep(50);  
  158.                         propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);  
  159.                     }  
  160.                     if (null != propertyValue)  
  161.                         property.SetValue(entityFromDB, propertyValue, null);  
  162.                 }  
  163.             }  
  164.   
  165.             entities.SaveChanges();  
  166.             return true;  
  167.         }  
  168.  
  169.         #endregion  
  170.  
  171.  
  172.         #region 获取相关的实体信息  
  173.         /// <summary>  
  174.         /// 分页_获取指定页的数据集合  
  175.         /// </summary>  
  176.         /// <typeparam name="T">泛型类型参数</typeparam>  
  177.         /// <param name="query">查询条件</param>  
  178.         /// <param name="pageSize">每页显示数量</param>  
  179.         /// <param name="pageNum">当前页号</param>  
  180.         /// <param name="pageTotal">总页数</param>  
  181.         /// <param name="datasTotal">总数据量</param>  
  182.         /// <returns></returns>  
  183.         public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, int pageSize, int pageNum, out int pageTotal, out int datasTotal) where T : class  
  184.         {  
  185.             var table = GetTable<T>();  
  186.   
  187.             datasTotal = (from t in table  
  188.                           select t).Where(query).Count();  
  189.   
  190.             pageTotal = datasTotal / pageSize + 1;  
  191.   
  192.             return (from t in table  
  193.                     select t).Where(query).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();  
  194.         }  
  195.         /// <summary>  
  196.         /// 分页_获取指定页的数据集合  
  197.         /// </summary>  
  198.         /// <typeparam name="T">泛型类型参数</typeparam>  
  199.         /// <param name="query">查询条件</param>  
  200.         /// <param name="pageSize">每页显示数量</param>  
  201.         /// <param name="pageNum">当前页号</param>  
  202.         /// <param name="pageTotal">总页数</param>  
  203.         /// <param name="datasTotal">总数据量</param>  
  204.         /// <returns></returns>  
  205.         public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, Func<T, object> orderByDesc, int pageSize, int pageNum, out int pageTotal, out int datasTotal) where T : class  
  206.         {  
  207.             var table = GetTable<T>();  
  208.   
  209.             datasTotal = (from t in table  
  210.                           select t).Where(query).Count();  
  211.   
  212.             pageTotal = (int)Math.Ceiling((double)datasTotal / pageSize);  
  213.             return (from t in table  
  214.                     select t).Where(query).OrderByDescending(orderByDesc).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();  
  215.         }  
  216.         /// <summary>  
  217.         /// 获取指定条件的实体集合  
  218.   
  219.         /// </summary>  
  220.         /// <typeparam name="T">泛型类型参数</typeparam>  
  221.         /// <param name="query">查询条件</param>  
  222.         /// <returns></returns>  
  223.         public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query) where T : class  
  224.         {  
  225.             var table = GetTable<T>();  
  226.             return (from t in table  
  227.                     select t).Where(query).ToList();  
  228.         }  
  229.   
  230.         /// <summary>  
  231.         /// 获取指定条件的实体集合  
  232.   
  233.         /// </summary>  
  234.         /// <typeparam name="T">泛型类型参数</typeparam>  
  235.         /// <param name="query">查询条件</param>        
  236.         /// <param name="orderAsc"></param>         
  237.         /// <returns></returns>  
  238.         public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, bool isAsc, Func<T, object> order) where T : class  
  239.         {  
  240.             var table = GetTable<T>();  
  241.             if (isAsc)  
  242.                 return (from t in table  
  243.                         select t).Where(query).OrderBy(order).ToList();  
  244.             return (from t in table  
  245.                     select t).Where(query).OrderByDescending(order).ToList();  
  246.         }  
  247.   
  248.         public virtual List<T> GetAllEntity<T>() where T : class  
  249.         {  
  250.             var table = GetTable<T>();  
  251.             return (from t in table  
  252.                     select t).ToList();  
  253.         }  
  254.         #endregion  
  255.  
  256.  
  257.         #region 新增实体  
  258.         /// <summary>  
  259.         /// 新增单个实体  
  260.         /// </summary>  
  261.         /// <typeparam name="T">泛型类型参数</typeparam>  
  262.         /// <param name="entity">待插入的实体</param>  
  263.         /// <returns></returns>  
  264.         public virtual void InsertEntity<T>(T entity) where T : class  
  265.         {  
  266.             var table = GetTable<T>();  
  267.             table.AddObject(entity);  
  268.             entities.SaveChanges();  
  269.         }  
  270.   
  271.         /// <summary>  
  272.         /// 批量新增实体  
  273.         /// </summary>  
  274.         /// <typeparam name="T">泛型类型参数</typeparam>  
  275.         /// <param name="entityList">待添加的实体集合</param>  
  276.         /// <returns></returns>  
  277.         public virtual void BatchInsertEntity<T>(List<T> entityList) where T : class  
  278.         {  
  279.             if (entityList.Count > 0)  
  280.             {  
  281.                 var table = GetTable<T>();  
  282.                 foreach (var item in entityList)  
  283.                 {  
  284.                     table.AddObject(item);//DeleteAllOnSubmit(toDeletedColl);  
  285.                 }  
  286.                 entities.SaveChanges();  
  287.             }  
  288.         }  
  289.         #endregion  
  290.  
  291.         #region 删除实体  
  292.         /// <summary>  
  293.         /// 根据条件删除指定实体  
  294.         /// </summary>  
  295.         /// <typeparam name="T">实体</typeparam>  
  296.         /// <param name="query">条件</param>  
  297.         /// <returns>bool</returns>  
  298.         public virtual void DeleteEntitys<T>(Expression<Func<T, bool>> query) where T : class  
  299.         {  
  300.             var table = GetTable<T>();  
  301.             var toDeletedColl = table.Where(query);  
  302.             if (toDeletedColl != null && toDeletedColl.Count() > 0)  
  303.             {  
  304.                 foreach (var item in toDeletedColl)  
  305.                 {  
  306.                     table.DeleteObject(item);//DeleteAllOnSubmit(toDeletedColl);  
  307.                 }  
  308.                 entities.SaveChanges();  
  309.             }  
  310.         }  
  311.         #endregion  
  312.     }  
  313. }  
posted @ 2014-12-30 15:14  关中秦人  阅读(172)  评论(0编辑  收藏  举报