/// <summary> /// 更新一个实体 /// </summary> /// <param name="entity"></param> /// <param name="primaryKeyField"></param> /// <returns></returns> public override int UpdateEntity(BaseEntity entity, string primaryKeyField = "") { Type t = entity.GetType(); PropertyInfo[] ps = t.GetProperties(); PropertyInfo pk; if (string.IsNullOrEmpty(primaryKeyField)) { pk = ps.FirstOrDefault(p => IdentityAttribute.GetAttribute(p) != null); } else { pk = ps.FirstOrDefault(p => p.Name == primaryKeyField); } if (pk == null) { throw new Exception(string.Format("实体{0}没有设置主键", t.FullName)); } string where = " WHERE " + pk.Name + "=@" + pk.Name; string updateSql = ""; //if (entity.Dbvalue.Count == 0) updateSql = GenerateUpdateSql(entity); //else // updateSql = GenerateUpdateSql(t, entity); if (string.IsNullOrWhiteSpace(updateSql)) { return(0); } updateSql += where; try { using (IDbConnection connection = OpenConnection(SqlExecuteType.Write)) { lock (syncObj) { var execSql = ToMySql(updateSql); OnExecutingCommand(execSql); int result = connection.Execute(execSql, entity, null, 60); OnExecutedCommand(execSql); if (CheckEnableCache(entity.GetType())) { CacheHelper.CacheService.Set( EntityUpdateTrackHelper.GetEntityKey(entity), entity, CachingExpirationType.SingleObject); } return(result); } } } catch (Exception ex) { throw new Exception("发生错误,SQL语句为:" + updateSql, ex); } }
/// <summary> /// 根据主键Id获取实体集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entityIds"></param> /// <returns></returns> public override List <T> GetListByEntityIds <T>(IEnumerable <int> entityIds) { T[] tArray = new T[entityIds.Count()]; IDictionary <int, int> objs = new Dictionary <int, int>(); for (int i = 0; i < entityIds.Count(); i++) { T entity = CacheHelper.CacheService.Get <T>(EntityUpdateTrackHelper.GetEntityKey(typeof(T), entityIds.ElementAt(i))); if (entity == null) { tArray[i] = default(T); objs[entityIds.ElementAt(i)] = i; } else { tArray[i] = entity; } } if (objs.Any()) { var entityType = typeof(T); var tableInfoAttr = TableInfoAttribute.GetAttribute(entityType); var pk = entityType.GetProperties().FirstOrDefault(p => IdentityAttribute.GetAttribute(p) != null); var tableName = tableInfoAttr == null ? entityType.Name : tableInfoAttr.TableName; if (pk != null) { var datalist = FetchEntities <T>(tableName + "." + pk.Name + " IN(" + objs.Keys.ToArray().GetString() + ")"); foreach (var entity1 in datalist) { tArray[objs[entity1.EntityId]] = entity1; CacheHelper.CacheService.Add(EntityUpdateTrackHelper.GetEntityKey(entity1), entity1, CachingExpirationType.SingleObject); } } } List <T> tEntities = new List <T>(); T[] tArray1 = tArray; for (int i = 0; i < tArray1.Length; i++) { T entity2 = tArray1[i]; if (entity2 != null) { tEntities.Add(entity2); } } return(tEntities); }
/// <summary> /// 根据Id获取实体 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="id">主键Id</param> /// <param name="primaryKeyField"></param> /// <returns></returns> public override T Get <T>(int id, string primaryKeyField = "") { //find key Type type = typeof(T); T entity; if (CheckEnableCache(type)) { entity = CacheHelper.CacheService.Get <T>(EntityUpdateTrackHelper.GetEntityKey(type, id)); if (entity != null) { return(entity); } } PropertyInfo pk; PropertyInfo[] ps = type.GetProperties(); if (string.IsNullOrEmpty(primaryKeyField)) { pk = GetPrimaryKey(type); } else { pk = ps.FirstOrDefault(p => p.Name == primaryKeyField); } if (pk == null) { throw new Exception(string.Format("实体{0}没有设置主键", type.FullName)); } TableInfoAttribute tableInfo = TableInfoAttribute.GetAttribute(type); string where = string.Format("{0}.{1}={2}", tableInfo == null ? type.Name : tableInfo.TableName, pk.Name, id); entity = GetList <T>(where).FirstOrDefault(); if (entity != null) { CacheHelper.CacheService.Add(EntityUpdateTrackHelper.GetEntityKey(type, id), entity, CachingExpirationType.SingleObject); } //if (entity != null) // foreach (var item in ps) // { // entity.Dbvalue.Add(new KeyValuePair<string, object>(item.Name, item.GetValue(entity, null))); // } return(entity); }
/// <summary> /// 添加一个实体 /// </summary> /// <param name="entity"></param> /// <returns></returns> public override int AddEntity(BaseEntity entity) { //判断是否启用了缓存 Type type = entity.GetType(); PropertyInfo[] propertyInfos = type.GetProperties(); var ps = new DynamicParameters { }; PropertyInfo pk = null; bool hasIdentityField = false; foreach (PropertyInfo propertyInfo in propertyInfos) { if (IdentityAttribute.GetAttribute(propertyInfo) != null) { pk = propertyInfo; hasIdentityField = true; } if (ExcludeFieldAttribute.GetAttribute(propertyInfo) != null) { continue; } ps.Add(propertyInfo.Name, propertyInfo.GetValue(entity, null)); } string insertSql = GenerateInsertSql(type); try { using (IDbConnection connection = OpenConnection(SqlExecuteType.Write)) { OnExecutingCommand(insertSql); decimal id = connection.Query <decimal>(insertSql, ps).FirstOrDefault(); OnExecutedCommand(insertSql); if (hasIdentityField) { pk.SetValue(entity, Convert.ToInt32(id), null); if (entity.IsEnableCache) { RetechWing.Infrastructure.Caching.CacheHelper.CacheService.Add( EntityUpdateTrackHelper.GetEntityKey(entity), entity, CachingExpirationType.SingleObject ); } } return(Convert.ToInt32(id)); } } catch (Exception ex) { throw new Exception("发生错误,SQL语句为:" + insertSql + "\r\n实体为:" + entity, ex); } }