首页 > 代码库 > 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 = http://www.mamicode.com/new DataCommon(ConnectionString);" * "; } 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 = http://www.mamicode.com/null;"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(); } } }
Entity Framework底层操作封装V2版本号(1)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。