首页 > 代码库 > Entity Framework底层操作封装V2版本(2)
Entity Framework底层操作封装V2版本(2)
这个类是真正的数据库操作类,上面的那个类只是调用了这个封装类的方法进行的操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using System.Data.Entity; using System.Data.Linq; using System.Data.Objects; using System.Reflection; using System.Threading; using System.Data; using System.Data.Objects.DataClasses; using JFrame.Utility; namespace JFrame.AccessCommon { public class DataCommon { static readonly string Connection = "name=EntitiesContainer"; static readonly string ContainerName = "EntitiesContainer"; ObjectContext entities; public DataCommon(string Connection, string ContainerName = "EntitiesContainer") { entities = new ObjectContext(Connection); entities.DefaultContainerName = ContainerName; } //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 { ObjectContext entitiesNew = new ObjectContext(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 ///<summary> ///获取主键,此方式只适用于edmx数据表结构 ///</summary> ///<param name="infos"></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) { return columnName = propertyInfo.Name; } } return columnName; } //protected override string GetEntitySetName() //{ // return context.Products.EntitySet.Name; //} #region 更新实体 public bool Update<T>(T entity, string PrimaryKey, object PrimaryKeyValue) where T : EntityObject { Type type = typeof(T); string strName = entities.Connection.ConnectionString.Replace("name=", ""); EntityKey key = null; try { entity.EntityKey = entities.CreateEntityKey(type.Name, entity); } catch (Exception ex) { } try { key = entity.EntityKey;//new EntityKey("Entities." + type.Name, PrimaryKey, PrimaryKeyValue); } catch (Exception ex) { } object propertyValue = http://www.mamicode.com/null;>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。