/// <summary> /// 根据主键删除一个实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="key">主键的值</param> public static void DeleteByKey <TEntity>(object key) where TEntity : SQLEntity { var keyInfo = EntityParser.GetKey(typeof(TEntity)); var keyName = keyInfo.KeyName; var sql = SqlBuilder.DeleteSQL(EntityParser.GetTableName(typeof(TEntity)), string.Format("{0} = @{0}", keyName)); SQLServerHelper.ExecuteNonQuery(sql, new SqlParameter("@" + keyName, key)); }
/// <summary> /// 将实体实例的属性的值更新到数据库中,必须指定主键 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <param name="updateToEntity"></param> /// <returns></returns> public static TEntity Update <TEntity>(TEntity entity, bool updateToEntity = true) where TEntity : SQLEntity { if (entity.EntityState == EntityState.NotInitialized) { throw new Exception("要更新的实体尚未初始化"); } else if (entity.EntityState == EntityState.NotChanged) { return(entity); } else { var entityType = typeof(TEntity); var key = EntityParser.GetKey(entityType); //找到主键属性 if (key == null) { throw new Exception("在实体类中没有找主键属性"); } List <SqlParameter> parameters = new List <SqlParameter>(); var tableName = EntityParser.GetTableName(entityType); StringBuilder update = new StringBuilder(); update.AppendFormat("UPDATE [{0}] SET ", tableName); var flags = false; foreach (var propertyName in entity) //遍历更改的属性 { if (propertyName == key.PropertyName) { continue; } var propertyInfo = entityType.GetProperty(propertyName); if (propertyInfo == null || EntityParser.IsIgnore(propertyInfo)) //如果找不到属性或不映射到数据库 { continue; } var sqlPair = SqlBuilder.ColumnSqlForUpdate(entity, propertyInfo, parameters); //获取更新列值的sql语句 if (string.IsNullOrEmpty(sqlPair)) { continue; } else { string columnName = EntityParser.GetColumnName(propertyInfo); update.AppendFormat("{0} = {1}, ", columnName, sqlPair); flags = true; } } if (flags) { update.Remove(update.Length - 2, 2).AppendFormat(" "); } else { return(entity); } update.AppendFormat(" WHERE {0} = @{0}", key.KeyName); var keyValue = key.Key.GetValue(entity, null); //获取主键的值 parameters.Add(new SqlParameter("@" + key.PropertyName, keyValue)); SQLServerHelper.ExecuteNonQuery(update.ToString(), parameters.ToArray()); //更新到数据库 entity.EntityState = EntityState.NotChanged; //未更改 if (updateToEntity) { //SingleByKey(keyValue, entity); //从数据库刷新属性 RefreshEntity(entity); } } return(entity); }
/// <summary> /// 向数据库中插入一个实体并返回实体类的主键的值,并从数据库中刷新实体的所有值 /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TEntity"></typeparam> /// <param name="entity">要插入到数据库的实体类的实例</param> /// <returns></returns> public static TEntity Insert <TEntity>(TEntity entity) where TEntity : SQLEntity { var entityType = typeof(TEntity); List <ColumnContext> contexts; if (!insertCache.TryGetValue(entityType, out contexts)) { contexts = new List <ColumnContext>(); foreach (var propertyInfo in entityType.GetProperties()) { if (EntityParser.IsIgnore(propertyInfo)) { continue; } var config = EntityParser.GetColumnConfig(propertyInfo); var context = new ColumnContext() { Config = config, PropertyInfo = propertyInfo, PropertyName = propertyInfo.Name, ColumnName = EntityParser.GetColumnName(propertyInfo) }; contexts.Add(context); } insertCache.Add(entityType, contexts); } List <SqlParameter> parameters = new List <SqlParameter>(); foreach (var context in contexts) { if (context.Config.UsePropertyForInsert) { var valueExpression = "@" + context.PropertyName; var propertyValue = context.PropertyInfo.GetValue(entity, null); if (context.Config.DBType == DBType.PropertyType) //如果直接用属性 { parameters.Add(new SqlParameter(valueExpression, propertyValue)); } else //如果要转换类型 { propertyValue = EntityParser.ConvertPropertyValue(context.PropertyInfo, propertyValue, context.Config.DBType); parameters.Add(new SqlParameter(valueExpression, propertyValue)); } } } var keyContext = EntityParser.GetKey(entityType); //寻找主键特性 object key; string sql = SqlBuilder.InsertSQL(entityType); if (keyContext.KeyConfig.UsePropertyForInsert) //如果使用属性的值插入到数据库 { SQLServerHelper.ExecuteNonQuery(sql, parameters.ToArray()); key = keyContext.Key.GetValue(entity, null); } else { key = SQLServerHelper.GetSingle(sql, parameters.ToArray()); //插入并获取主键的值 } var se = entity as SQLEntity; if (se != null) { se.EntityState = EntityState.NotInitialized; //未初始化 } string selectSQL = SqlBuilder.SelectSQL(typeof(TEntity), string.Format("{0} = @{0}", keyContext.PropertyName)); SQLServerHelper.FirstOrDefault((reader) => { return(FilldEntity <TEntity>(reader, entity)); }, selectSQL, new SqlParameter("@" + keyContext.PropertyName, key)); //从数据库中刷新实体 if (se != null) { se.EntityState = EntityState.NotChanged; //未修改 } return(entity); }
/// <summary> /// 根据指定的条件删除多个实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="where">条件</param> /// <param name="pms">参数</param> public static void Delete <TEntity>(string where, params SqlParameter[] pms) where TEntity : SQLEntity { var sql = SqlBuilder.DeleteSQL(EntityParser.GetTableName(typeof(TEntity)), where); SQLServerHelper.ExecuteNonQuery(sql, pms); }