Entity Framework底层操作封装V2版本(1)
Entity Framework底层操作封装V2版本(1)
因为同志们一直给我提建议说,以前发的版本有问题。所以经过了我这一年多的使用和扩展,现在方法基本稳定了。现在贴出来给大家使用:
首先上场的是数据库操作层:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Linq.Expressions;
- using System.Data.Objects.DataClasses;
- using System.Reflection;
- using JFrame.Utility;
- namespace JFrame.AccessCommon
- {
- public class AccessBase<T> where T : EntityObject
- {
- protected string _TableName;
- protected string _QueryColums;
- protected string _PrimaryKey;
- protected Type _PrimaryKeyType;
- /// <summary>
- /// 执行数据库操作基础类方法
- /// </summary>
- protected DataCommon Data;
- /// <summary>
- /// 实例化操作对象
- /// </summary>
- /// <param name="ConnectionString">EF连接字符串</param>
- /// <param name="PrimaryKey">主键</param>
- /// <param name="strTableName">表名</param>
- /// <param name="QueryColums">查询的列</param>
- /// <param name="IsEntityData">是否为ADO实体对象</param>
- public AccessBase(string ConnectionString, string PrimaryKey = "", string strTableName = "", string QueryColums = "")
- {
- Data = new DataCommon(ConnectionString);
- Type t = typeof(T);
- if (string.IsNullOrEmpty(strTableName))
- {
- strTableName = t.Name; //GetType(t).ToString();
- }
- _TableName = strTableName;
- if (string.IsNullOrEmpty(QueryColums))
- {
- _QueryColums = " * ";
- }
- if (string.IsNullOrEmpty(PrimaryKey))
- {
- PropertyInfo[] infos = t.GetProperties();
- PrimaryKey = GetPrimaryKey(infos);
- }
- _PrimaryKey = PrimaryKey;
- }
- /// <summary>
- /// 获取主键
- /// </summary>
- /// <param name="infos"></param>
- /// <param name="IsEdmScalarPropertyAttribute"></param>
- /// <returns></returns>
- private string GetPrimaryKey(PropertyInfo[] infos)
- {
- string columnName = string.Empty;
- foreach (PropertyInfo propertyInfo in infos)
- {
- object[] customInfos = propertyInfo.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), true);
- if (customInfos == null
- || customInfos.Length == 0)
- return string.Empty;
- EdmScalarPropertyAttribute limit = customInfos.GetValue(0) as EdmScalarPropertyAttribute;
- if (limit.EntityKeyProperty)
- {
- _PrimaryKeyType = propertyInfo.PropertyType;
- return columnName = propertyInfo.Name;
- }
- }
- return columnName;
- }
- /// <summary>
- /// 增加单个实体
- /// </summary>
- /// <param name="t"></param>
- public virtual void AddEntity(T t)
- {
- Data.InsertEntity<T>(t);
- }
- /// <summary>
- /// 获取单个实体
- /// </summary>
- /// <param name="query">查询条件</param>
- /// <returns></returns>
- public virtual T GetSingleEntity(Expression<Func<T, bool>> query)
- {
- return Data.GetSingleEntity<T>(query);
- }
- public virtual List<T> GetAllEntityByPage(Expression<Func<T, bool>> query,PagingInfo PageInfo, Func<T, object> orderByDesc)
- {
- return Data.GetAllEntity<T>(query, PageInfo, orderByDesc);
- }
- /// <summary>
- /// 获取单个实体
- /// </summary>
- /// <typeparam name="T">泛型类型参数</typeparam>
- /// <param name="express">查询条件</param>
- /// <returns></returns>
- public virtual T TryGetSingleEntity(Expression<Func<T, bool>> query)
- {
- try
- {
- return Data.GetSingleEntity<T>(query);
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- /// <summary>
- /// 根据条件获取单个实体
- /// </summary>
- /// <param name="query">查询条件</param>
- /// <returns>实体对象</returns>
- public virtual T GetSingleEntity(string query)
- {
- return GetListByWhere(query).FirstOrDefault();
- }
- /// <summary>
- /// 根据主键获取对象
- /// </summary>
- /// <param name="PrimaryKeyValue">主键值</param>
- /// <returns>对象</returns>
- public virtual T GetSingleEntity(object PrimaryKeyValue)
- {
- StringBuilder strWhere = new StringBuilder();
- switch (_PrimaryKeyType.Name.ToLower())
- {
- case "int16":
- case "int32":
- case "int64":
- case "int":
- case "decimal":
- case "double":
- case "float":
- case "short":
- strWhere.AppendFormat(" {0}={1}", _PrimaryKey, PrimaryKeyValue);
- break;
- case "bool":
- case "boolean":
- if ((bool)PrimaryKeyValue)
- { strWhere.AppendFormat(" {0}=1", _PrimaryKey); }
- else
- { strWhere.AppendFormat(" {0}=0", _PrimaryKey); }
- break;
- default:
- strWhere.AppendFormat(" {0}='{1}'", _PrimaryKey, PrimaryKeyValue);
- break;
- }
- return GetListByWhere(strWhere.ToString()).FirstOrDefault();
- }
- /// <summary>
- /// 修改单个实体
- /// </summary>
- /// <param name="t"></param>
- public virtual void UpdateEntity(T t)
- {
- Type type = typeof(T);
- PropertyInfo[] infos = type.GetProperties();
- PropertyInfo info = infos.Where(p => p.Name == _PrimaryKey).FirstOrDefault();
- object value = null;
- if (info != null)
- {
- value = info.GetValue(t, null);
- }
- else
- {
- return;
- }
- Data.Update<T>(t, _PrimaryKey, value);
- }
- /// <summary>
- /// 根据条件删除信息
- /// </summary>
- /// <param name="query">条件</param>
- public virtual void Delete(Expression<Func<T, bool>> query)
- {
- Data.DeleteEntitys<T>(query);
- }
- /// <summary>
- /// 根据条件获取相关信息表
- /// </summary>
- /// <param name="strWhere">Where条件</param>
- /// <returns>数据集合</returns>
- public virtual List<T> GetListByWhere(string strWhere)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.AppendFormat("select {1} from {0}", _TableName, _QueryColums);
- if (!string.IsNullOrEmpty(strWhere))
- {
- strSql.AppendFormat(" where {0}", strWhere);
- }
- return Data.ExecuteQuery<T>(strSql.ToString()).ToList();
- }
- public virtual List<T> GetListByWhere()
- {
- return GetListByWhere(string.Empty);
- }
- /// <summary>
- /// 根据条件获取相关信息
- /// </summary>
- /// <param name="strTableName">表名</param>
- /// <param name="strColums">相关列(前面可加Top)</param>
- /// <param name="strWhere">Where条件</param>
- /// <returns>数据集合</returns>
- public virtual List<T> GetListByWhere(string strTableName,string strColums,string strWhere,string strOrderBy)
- {
- StringBuilder strSql = new StringBuilder();
- strSql.AppendFormat("select {1} from {0}", strTableName, strColums);
- if (!string.IsNullOrEmpty(strWhere))
- {
- strSql.AppendFormat(" where {0}", strWhere);
- }
- if (!string.IsNullOrEmpty(strOrderBy))
- {
- strSql.AppendFormat(" Order by {0}", strOrderBy);
- }
- return Data.ExecuteQuery<T>(strSql.ToString()).ToList();
- }
- /// <summary>
- /// 根据条件获取相关监测信息表
- /// </summary>
- /// <param name="strWhere">Where条件</param>
- /// <returns>数据集合</returns>
- public virtual List<T> GetListByWhere(Expression<Func<T, bool>> query)
- {
- return Data.GetAllEntity<T>(query);
- }
- /// <summary>
- /// 获取列最大值
- /// </summary>
- /// <param name="CloumnName">列名</param>
- /// <returns>最大值</returns>
- public virtual object GetCloumnNameMaxValue<T2>(string CloumnName)
- {
- if (string.IsNullOrEmpty(CloumnName))
- {
- throw new Exception("列名值不能为空");
- }
- StringBuilder strSql = new StringBuilder();
- strSql.AppendFormat("select max({1}) as val from {0}", _TableName, CloumnName);
- return Data.ExecuteQuery<T2>(strSql.ToString()).FirstOrDefault();
- }
- /// <summary>
- /// 获取最大主键
- /// </summary>
- /// <returns></returns>
- protected virtual int? GetMaxPrimaryKey()
- {
- StringBuilder strSql = new StringBuilder();
- strSql.AppendFormat("select max({1}) from {0}", _TableName, _PrimaryKey);
- return Data.ExecuteQuery<int>(strSql.ToString()).FirstOrDefault();
- }
- }
- }