Entity Framework底层操作封装V2版本(1)

因为同志们一直给我提建议说,以前发的版本有问题。所以经过了我这一年多的使用和扩展,现在方法基本稳定了。现在贴出来给大家使用:

首先上场的是数据库操作层:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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.Objects.DataClasses;  
  7. using System.Reflection;  
  8. using JFrame.Utility;  
  9. namespace JFrame.AccessCommon  
  10. {  
  11.     public class AccessBase<T> where T : EntityObject  
  12.     {  
  13.         protected string _TableName;  
  14.         protected string _QueryColums;  
  15.         protected string _PrimaryKey;  
  16.         protected Type _PrimaryKeyType;  
  17.   
  18.   
  19.   
  20.         /// <summary>  
  21.         /// 执行数据库操作基础类方法  
  22.         /// </summary>  
  23.         protected DataCommon Data;  
  24.   
  25.   
  26.   
  27.         /// <summary>  
  28.         /// 实例化操作对象  
  29.         /// </summary>  
  30.         /// <param name="ConnectionString">EF连接字符串</param>  
  31.         /// <param name="PrimaryKey">主键</param>  
  32.         /// <param name="strTableName">表名</param>  
  33.         /// <param name="QueryColums">查询的列</param>  
  34.         /// <param name="IsEntityData">是否为ADO实体对象</param>  
  35.         public AccessBase(string ConnectionString, string PrimaryKey = "", string strTableName = "", string QueryColums = "")  
  36.         {  
  37.             Data = new DataCommon(ConnectionString);  
  38.             Type t = typeof(T);  
  39.             if (string.IsNullOrEmpty(strTableName))  
  40.             {  
  41.                 strTableName = t.Name; //GetType(t).ToString();  
  42.             }  
  43.             _TableName = strTableName;  
  44.             if (string.IsNullOrEmpty(QueryColums))  
  45.             {  
  46.                 _QueryColums = " * ";  
  47.             }  
  48.   
  49.             if (string.IsNullOrEmpty(PrimaryKey))  
  50.             {  
  51.                 PropertyInfo[] infos = t.GetProperties();  
  52.                 PrimaryKey = GetPrimaryKey(infos);  
  53.             }  
  54.             _PrimaryKey = PrimaryKey;  
  55.         }  
  56.   
  57.   
  58.         
  59.   
  60.   
  61.         /// <summary>  
  62.         /// 获取主键  
  63.         /// </summary>  
  64.         /// <param name="infos"></param>  
  65.         /// <param name="IsEdmScalarPropertyAttribute"></param>  
  66.         /// <returns></returns>  
  67.         private string GetPrimaryKey(PropertyInfo[] infos)  
  68.         {  
  69.             string columnName = string.Empty;  
  70.             foreach (PropertyInfo propertyInfo in infos)  
  71.             {  
  72.                 object[] customInfos = propertyInfo.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), true);  
  73.                 if (customInfos == null  
  74.                || customInfos.Length == 0)  
  75.                     return string.Empty;  
  76.   
  77.                 EdmScalarPropertyAttribute limit = customInfos.GetValue(0) as EdmScalarPropertyAttribute;  
  78.                 if (limit.EntityKeyProperty)  
  79.                 {  
  80.                     _PrimaryKeyType = propertyInfo.PropertyType;  
  81.                     return columnName = propertyInfo.Name;  
  82.                 }  
  83.             }  
  84.             return columnName;  
  85.   
  86.         }  
  87.   
  88.   
  89.   
  90.   
  91.         /// <summary>  
  92.         /// 增加单个实体  
  93.         /// </summary>  
  94.         /// <param name="t"></param>  
  95.         public virtual void AddEntity(T t)  
  96.         {  
  97.             Data.InsertEntity<T>(t);  
  98.         }  
  99.   
  100.         /// <summary>  
  101.         /// 获取单个实体  
  102.         /// </summary>  
  103.         /// <param name="query">查询条件</param>  
  104.         /// <returns></returns>  
  105.         public virtual T GetSingleEntity(Expression<Func<T, bool>> query)  
  106.         {  
  107.             return Data.GetSingleEntity<T>(query);  
  108.         }  
  109.   
  110.   
  111.         public virtual List<T> GetAllEntityByPage(Expression<Func<T, bool>> query,PagingInfo PageInfo, Func<T, object> orderByDesc)  
  112.         {  
  113.            return Data.GetAllEntity<T>(query, PageInfo,  orderByDesc);  
  114.         }  
  115.   
  116.   
  117.   
  118.          
  119.   
  120.         /// <summary>  
  121.         /// 获取单个实体  
  122.         /// </summary>  
  123.         /// <typeparam name="T">泛型类型参数</typeparam>  
  124.         /// <param name="express">查询条件</param>  
  125.         /// <returns></returns>  
  126.         public virtual T TryGetSingleEntity(Expression<Func<T, bool>> query)  
  127.         {  
  128.             try  
  129.             {  
  130.                 return Data.GetSingleEntity<T>(query);  
  131.             }  
  132.             catch (Exception ex)  
  133.             {  
  134.                 return null;  
  135.             }  
  136.         }  
  137.   
  138.   
  139.         /// <summary>  
  140.         /// 根据条件获取单个实体  
  141.         /// </summary>  
  142.         /// <param name="query">查询条件</param>  
  143.         /// <returns>实体对象</returns>  
  144.         public virtual T GetSingleEntity(string query)  
  145.         {  
  146.             return GetListByWhere(query).FirstOrDefault();  
  147.         }  
  148.   
  149.   
  150.         /// <summary>  
  151.         /// 根据主键获取对象  
  152.         /// </summary>  
  153.         /// <param name="PrimaryKeyValue">主键值</param>  
  154.         /// <returns>对象</returns>  
  155.         public virtual T GetSingleEntity(object PrimaryKeyValue)  
  156.         {  
  157.             StringBuilder strWhere = new StringBuilder();  
  158.             switch (_PrimaryKeyType.Name.ToLower())  
  159.             {  
  160.                 case "int16":  
  161.                 case "int32":  
  162.                 case "int64":  
  163.                 case "int":  
  164.                 case "decimal":  
  165.                 case "double":  
  166.                 case "float":  
  167.                 case "short":  
  168.                     strWhere.AppendFormat(" {0}={1}", _PrimaryKey, PrimaryKeyValue);  
  169.                     break;  
  170.                 case "bool":  
  171.                 case "boolean":  
  172.                     if ((bool)PrimaryKeyValue)  
  173.                     { strWhere.AppendFormat(" {0}=1", _PrimaryKey); }  
  174.                     else  
  175.                     { strWhere.AppendFormat(" {0}=0", _PrimaryKey); }  
  176.   
  177.                     break;  
  178.                 default:  
  179.                     strWhere.AppendFormat(" {0}='{1}'", _PrimaryKey, PrimaryKeyValue);  
  180.                     break;  
  181.             }  
  182.   
  183.             return GetListByWhere(strWhere.ToString()).FirstOrDefault();  
  184.         }  
  185.   
  186.         /// <summary>  
  187.         /// 修改单个实体  
  188.         /// </summary>  
  189.         /// <param name="t"></param>  
  190.         public virtual void UpdateEntity(T t)  
  191.         {  
  192.             Type type = typeof(T);  
  193.             PropertyInfo[] infos = type.GetProperties();  
  194.             PropertyInfo info = infos.Where(p => p.Name == _PrimaryKey).FirstOrDefault();  
  195.             object value = null;  
  196.             if (info != null)  
  197.             {  
  198.                 value = info.GetValue(t, null);  
  199.             }  
  200.             else  
  201.             {  
  202.                 return;  
  203.             }  
  204.             Data.Update<T>(t, _PrimaryKey, value);  
  205.         }  
  206.   
  207.   
  208.         /// <summary>  
  209.         /// 根据条件删除信息  
  210.         /// </summary>  
  211.         /// <param name="query">条件</param>  
  212.         public virtual void Delete(Expression<Func<T, bool>> query)  
  213.         {  
  214.             Data.DeleteEntitys<T>(query);  
  215.         }  
  216.   
  217.   
  218.         /// <summary>  
  219.         /// 根据条件获取相关信息表  
  220.         /// </summary>  
  221.         /// <param name="strWhere">Where条件</param>  
  222.         /// <returns>数据集合</returns>  
  223.         public virtual List<T> GetListByWhere(string strWhere)  
  224.         {  
  225.             StringBuilder strSql = new StringBuilder();  
  226.             strSql.AppendFormat("select {1} from {0}", _TableName, _QueryColums);  
  227.             if (!string.IsNullOrEmpty(strWhere))  
  228.             {  
  229.                 strSql.AppendFormat(" where {0}", strWhere);  
  230.             }  
  231.             return Data.ExecuteQuery<T>(strSql.ToString()).ToList();  
  232.         }  
  233.   
  234.   
  235.         
  236.         public virtual List<T> GetListByWhere()  
  237.         {  
  238.            return GetListByWhere(string.Empty);  
  239.         }   
  240.           
  241.         /// <summary>  
  242.         /// 根据条件获取相关信息  
  243.         /// </summary>  
  244.         /// <param name="strTableName">表名</param>  
  245.         /// <param name="strColums">相关列(前面可加Top)</param>  
  246.         /// <param name="strWhere">Where条件</param>  
  247.         /// <returns>数据集合</returns>  
  248.         public virtual List<T> GetListByWhere(string strTableName,string strColums,string strWhere,string strOrderBy)  
  249.         {  
  250.   
  251.             StringBuilder strSql = new StringBuilder();  
  252.             strSql.AppendFormat("select  {1}  from {0}", strTableName, strColums);  
  253.             if (!string.IsNullOrEmpty(strWhere))  
  254.             {  
  255.                 strSql.AppendFormat(" where {0}", strWhere);  
  256.             }  
  257.   
  258.             if (!string.IsNullOrEmpty(strOrderBy))  
  259.             {  
  260.                 strSql.AppendFormat(" Order by {0}", strOrderBy);  
  261.             }  
  262.   
  263.             return Data.ExecuteQuery<T>(strSql.ToString()).ToList();  
  264.         }  
  265.   
  266.         
  267.         /// <summary>  
  268.         /// 根据条件获取相关监测信息表  
  269.         /// </summary>  
  270.         /// <param name="strWhere">Where条件</param>  
  271.         /// <returns>数据集合</returns>  
  272.         public virtual List<T> GetListByWhere(Expression<Func<T, bool>> query)  
  273.         {  
  274.             return Data.GetAllEntity<T>(query);  
  275.         }  
  276.   
  277.   
  278.         /// <summary>  
  279.         /// 获取列最大值  
  280.         /// </summary>  
  281.         /// <param name="CloumnName">列名</param>  
  282.         /// <returns>最大值</returns>  
  283.         public virtual object GetCloumnNameMaxValue<T2>(string CloumnName)  
  284.         {  
  285.             if (string.IsNullOrEmpty(CloumnName))  
  286.             {  
  287.                 throw new Exception("列名值不能为空");  
  288.             }  
  289.             StringBuilder strSql = new StringBuilder();  
  290.             strSql.AppendFormat("select max({1}) as val from {0}", _TableName, CloumnName);  
  291.             return Data.ExecuteQuery<T2>(strSql.ToString()).FirstOrDefault();  
  292.   
  293.         }  
  294.   
  295.         /// <summary>  
  296.         /// 获取最大主键  
  297.         /// </summary>  
  298.         /// <returns></returns>  
  299.         protected virtual int? GetMaxPrimaryKey()  
  300.         {  
  301.             StringBuilder strSql = new StringBuilder();  
  302.             strSql.AppendFormat("select max({1}) from {0}", _TableName, _PrimaryKey);  
  303.             return Data.ExecuteQuery<int>(strSql.ToString()).FirstOrDefault();  
  304.         }  
  305.     }  
  306. }  
posted @ 2014-12-30 14:28  关中秦人  阅读(177)  评论(0编辑  收藏  举报