最近作移動的項目,要求底層數據庫是Oracle,可是由於本機是Sqlserver的環境。想了下,因而使用Entity Framework 進行開發,在開發完成之後切換到Oracle環境。想了下,就決定把之前封裝的Linq的底層操做類進行了修改,造成EF的底層操做類。這樣在本身熟悉的環境下,效率也會高不少,及時之後這個很差用,我切換起來也會簡單不少。多的不過了,各位看官上代碼,首先上的第一個類是底層操做的類。sql
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Data.Entity; using NOAS.PublicOpinionMonitor.ENTITY; using System.Data.Linq; using System.Data.Objects; using System.Reflection; using System.Threading; namespace NOAS.PublicOpinionMonitor.Access.Common { public class DataCommon { static readonly string Connection = "name=POMonitorDBEntities"; POMonitorDBEntities entities = new ENTITY.POMonitorDBEntities(Connection); #region 根據SQL語句查詢數據 public IEnumerable<T> ExecuteQuery<T>(string conn, string query, params object[] parms) { try { if (string.IsNullOrEmpty(conn)) { return entities.ExecuteStoreQuery<T>(query, parms); } else { DataContext myDC = new DataContext(conn); return myDC.ExecuteQuery<T>(query, parms); } } catch (Exception ex) { throw new Exception(ex.Message); } } public IEnumerable<T> ExecuteQuery<T>(string query) { return ExecuteQuery<T>(string.Empty, query, new object[] { }); } #endregion #region 執行操做類型SQl語句 /// <summary> /// 執行SQL命令 /// </summary> /// <param name="sqlCommand"></param> /// <returns></returns> public int ExecuteSqlCommand(string sqlCommand, string connection = null) { if (string.IsNullOrEmpty(connection)) return entities.ExecuteStoreCommand(sqlCommand); //ExecuteCommand(sqlCommand); else { POMonitorDBEntities entitiesNew = new POMonitorDBEntities(connection); return entitiesNew.ExecuteStoreCommand(sqlCommand); } } /// <summary> /// 執行SQL命令 /// </summary> /// <param name="sqlCommand"></param> /// <returns></returns> public void ExecuteSqlCommand(string sqlCommand) { ExecuteSqlCommand(connection: string.Empty, sqlCommand: sqlCommand); } #endregion #region 私有方法 private ObjectSet<T> GetTable<T>() where T : class { try { ObjectSet<T> customers = entities.CreateObjectSet<T>(); return customers; } catch (Exception ex) { throw ex; } } #endregion #region 統計指定條件的數據量 /// <summary> /// 統計指定條件的數據量 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="query"></param> /// <returns></returns> public virtual int Count<T>(Expression<Func<T, bool>> query) where T : class { var table = GetTable<T>(); return (from t in table select t).Where(query).Count(); } #endregion #region 獲取單個實體 /// <summary> /// 獲取單個實體 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="express">查詢條件</param> /// <returns></returns> public virtual T GetSingleEntity<T>(Expression<Func<T, bool>> query) where T : class { try { var table = GetTable<T>(); return (from t in table select t).Where(query).SingleOrDefault(); } catch (Exception ex) { throw ex; } } #endregion #region 更新實體 /// <summary> /// 更新實體 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="entity">更改後的實體</param> public virtual bool Update<T>(T entity) where T : class { object propertyValue = null; T entityFromDB = GetSingleEntity<T>(s => s == entity); if (null == entityFromDB) return false; PropertyInfo[] properties = entityFromDB.GetType().GetProperties(); foreach (PropertyInfo property in properties) { propertyValue = null; if (null != property.GetSetMethod()) { PropertyInfo entityProperty = entity.GetType().GetProperty(property.Name); if (entityProperty.PropertyType.BaseType == Type.GetType("System.ValueType") || entityProperty.PropertyType == Type.GetType("System.String")) propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null); if (propertyValue == null) { Thread.Sleep(50); propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null); } if (null != propertyValue) property.SetValue(entityFromDB, propertyValue, null); } } entities.SaveChanges(); return true; } #endregion #region 獲取相關的實體信息 /// <summary> /// 分頁_獲取指定頁的數據集合 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="query">查詢條件</param> /// <param name="pageSize">每頁顯示數量</param> /// <param name="pageNum">當前頁號</param> /// <param name="pageTotal">總頁數</param> /// <param name="datasTotal">總數據量</param> /// <returns></returns> public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, int pageSize, int pageNum, out int pageTotal, out int datasTotal) where T : class { var table = GetTable<T>(); datasTotal = (from t in table select t).Where(query).Count(); pageTotal = datasTotal / pageSize + 1; return (from t in table select t).Where(query).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList(); } /// <summary> /// 分頁_獲取指定頁的數據集合 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="query">查詢條件</param> /// <param name="pageSize">每頁顯示數量</param> /// <param name="pageNum">當前頁號</param> /// <param name="pageTotal">總頁數</param> /// <param name="datasTotal">總數據量</param> /// <returns></returns> 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 { var table = GetTable<T>(); datasTotal = (from t in table select t).Where(query).Count(); pageTotal = (int)Math.Ceiling((double)datasTotal / pageSize); return (from t in table select t).Where(query).OrderByDescending(orderByDesc).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList(); } /// <summary> /// 獲取指定條件的實體集合 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="query">查詢條件</param> /// <returns></returns> public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query) where T : class { var table = GetTable<T>(); return (from t in table select t).Where(query).ToList(); } /// <summary> /// 獲取指定條件的實體集合 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="query">查詢條件</param> /// <param name="orderAsc"></param> /// <returns></returns> public virtual List<T> GetAllEntity<T>(Expression<Func<T, bool>> query, bool isAsc, Func<T, object> order) where T : class { var table = GetTable<T>(); if (isAsc) return (from t in table select t).Where(query).OrderBy(order).ToList(); return (from t in table select t).Where(query).OrderByDescending(order).ToList(); } public virtual List<T> GetAllEntity<T>() where T : class { var table = GetTable<T>(); return (from t in table select t).ToList(); } #endregion #region 新增實體 /// <summary> /// 新增單個實體 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="entity">待插入的實體</param> /// <returns></returns> public virtual void InsertEntity<T>(T entity) where T : class { var table = GetTable<T>(); table.AddObject(entity); entities.SaveChanges(); } /// <summary> /// 批量新增實體 /// </summary> /// <typeparam name="T">泛型類型參數</typeparam> /// <param name="entityList">待添加的實體集合</param> /// <returns></returns> public virtual void BatchInsertEntity<T>(List<T> entityList) where T : class { if (entityList.Count > 0) { var table = GetTable<T>(); foreach (var item in entityList) { table.AddObject(item);//DeleteAllOnSubmit(toDeletedColl); } entities.SaveChanges(); } } #endregion #region 刪除實體 /// <summary> /// 根據條件刪除指定實體 /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="query">條件</param> /// <returns>bool</returns> public virtual void DeleteEntitys<T>(Expression<Func<T, bool>> query) where T : class { var table = GetTable<T>(); var toDeletedColl = table.Where(query); if (toDeletedColl != null && toDeletedColl.Count() > 0) { foreach (var item in toDeletedColl) { table.DeleteObject(item);//DeleteAllOnSubmit(toDeletedColl); } entities.SaveChanges(); } } #endregion } }