/// <summary> /// Обновить поля в записи /// </summary> /// <param name="model">Запись</param> /// <param name="fields">Список полей</param> /// <exception cref="ArgumentException">Обновляемое поле не может быть типа enum</exception> public virtual void UpdateFields(T model, params Expression <Func <T, object> >[] fields) { IUpdatable <T> update = _table.Where(IdQuery(model)).AsUpdatable(); foreach (var field in fields) { var value = field.Compile()(model); // Так как для драйвера npgsql не всё равно передается ли ему Enum или (object)Enum, // а так как expression как раз возвращает (object)Enum и не нашлось способа присести его к Enum // выбрасываем исключение при попытки обновить поле типа Enum if (value is Enum) { var message = @"Обновляемое поле не может быть типа enum Для обновление поля типа enum используйте конструкцию: Essence.AsQuerible() .Where(x => x.Id == DbModel.Id) .Set(x => x.Enum, DbModel.Enum) ... .Update();"; throw new ArgumentException(message, "fields"); } update = update.Set(field, value); } update.Update(); }
public bool UpdateJobMetadataValues( IDictionary <Expression <Func <SqlCommonDbOddJobMetaData, object> >, object> setters, Guid jobGuid, string oldStatusIfRequired) { using (var conn = _jobQueueConnectionFactory.CreateDataConnection(_mappingSchema.MappingSchema)) { var updatable = QueueTable(conn) .Where(q => q.JobGuid == jobGuid); if (String.IsNullOrWhiteSpace(oldStatusIfRequired) == false) { updatable = updatable.Where(q => q.Status == oldStatusIfRequired); } IUpdatable <SqlCommonDbOddJobMetaData> updateCommand = null; foreach (var set in setters) { updateCommand = (updateCommand == null) ? updatable.Set(set.Key, set.Value) : updateCommand.Set(set.Key, set.Value); } if (updateCommand != null) { return(updateCommand.Update() > 0); } return(false); } }
/// <summary> /// Update Job Metadata and Parameters via a Built command. /// </summary> /// <param name="commandData">A <see cref="JobUpdateCommand"/> with Criteria for updating.</param> /// <returns></returns> /// <remarks>This is designed to be a 'safe-ish' update. If done in a <see cref="TransactionScope"/>, /// You can roll-back if this returns false. If no <see cref="TransactionScope"/> is active, /// you could get dirty writes under some very edgy cases. /// </remarks> public bool UpdateJobMetadataAndParameters(JobUpdateCommand commandData) { //TODO: Make this even safer. using (var conn = _jobQueueConnectionFactory.CreateDataConnection(_mappingSchema.MappingSchema)) { bool ableToUpdateJob = true; var updatable = QueueTable(conn) .Where(q => q.JobGuid == commandData.JobGuid); if (String.IsNullOrWhiteSpace(commandData.OldStatusIfRequired) == false) { updatable = updatable.Where(q => q.Status == commandData.OldStatusIfRequired); } IUpdatable <SqlCommonDbOddJobMetaData> updateCommand = null; foreach (var set in commandData.SetJobMetadata) { updateCommand = (updateCommand == null) ? updatable.Set(set.Key, set.Value) : updateCommand.Set(set.Key, set.Value); } if (updateCommand != null) { ableToUpdateJob = updateCommand.Update() > 0; } else { ableToUpdateJob = updatable.Any(); } if (ableToUpdateJob && commandData.SetJobParameters != null && commandData.SetJobParameters.Count > 0) { foreach (var jobParameter in commandData.SetJobParameters) { var updatableParam = ParamTable(conn) .Where(q => q.JobGuid == commandData.JobGuid && q.ParamOrdinal == jobParameter.Key && ableToUpdateJob); IUpdatable <SqlCommonOddJobParamMetaData> updateParamCommand = null; foreach (var updatePair in jobParameter.Value) { updateParamCommand = (updateParamCommand == null) ? updatableParam.Set(updatePair.Key, updatePair.Value) : updateParamCommand.Set(updatePair.Key, updatePair.Value); } if (updateParamCommand != null) { var updatedRows = updateParamCommand.Update(); if (updatedRows == 0) { return(false); } } } } return(ableToUpdateJob); } }
public static Task <int> UpAsync <TSource>(this IUpdatable <TSource> uper) where TSource : EntityBase, new() { return(uper.Set(e => e.UpdateTime, DateTime.Now) .UpdateAsync()); }