public int Commit() { var affectedRecords = _context.SaveChanges(); _transaction?.Commit(); return(affectedRecords); }
/// <inheritdoc/> public void CommitTransaction() { try { SaveChanges(); transaction?.Commit(); ReleaseCurrentTransaction(); } catch { RollBackTransaction(); throw; } }
public int Commit() { try { var i = _context.SaveChanges(); _transaction?.Commit(); return(i); } catch (Exception ex) { throw ex; } }
private int Execute(Tuple <IEnumerable <ModificationCommandBatch>, IRelationalConnection> parameters) { var commandBatches = parameters.Item1; var connection = parameters.Item2; var rowsAffected = 0; IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null && CurrentContext.Context.Database.AutoTransactionsEnabled) { startedTransaction = connection.BeginTransaction(); } else { connection.Open(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return(rowsAffected); }
public virtual async Task <int> ExecuteAsync( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection, CancellationToken cancellationToken = default(CancellationToken)) { var rowsAffected = 0; await connection.OpenAsync(cancellationToken); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { await commandbatch.ExecuteAsync(connection, cancellationToken); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return(rowsAffected); }
public override async Task <int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { int result; var timestamp = _systemClock.UtcNow; IDbContextTransaction transaction = null; if (Database.CurrentTransaction == null && Database.GetEnlistedTransaction() == null && Transaction.Current == null) { transaction = await Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false); } using (transaction) { result = await base.SaveChangesAsync(false, cancellationToken).ConfigureAwait(false); var changes = ChangeTracker.Entries() .Where(_ => _.ShouldAuditChanges()) .Select(entry => CreateChangeEvent(timestamp, entry)) .ToList(); ChangeTracker.AcceptAllChanges(); await ChangeEvents.AddRangeAsync(changes, cancellationToken).ConfigureAwait(false); result += await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false); transaction?.Commit(); } return(result); }
public new int SaveChanges(IDbContextTransaction transaction = null) { var ret = base.SaveChanges(); transaction?.Commit(); return(ret); }
public void CloseTransaction(Exception exception) { try { if (exception != null) { _currentTransaction?.Rollback(); return; } SaveChanges(); _currentTransaction?.Commit(); } catch (Exception ex) { Log.Error(ex, "Exception thrown while attempting to close a transaction."); _currentTransaction?.Rollback(); throw; } finally { _currentTransaction?.Dispose(); _currentTransaction = null; } }
public virtual void Commit() { SaveChanges(); _transaction?.Commit(); _transaction?.Dispose(); _transaction = null; }
public virtual int Execute( IEnumerable <ModificationCommandBatch> commandBatches, IRelationalConnection connection) { var rowsAffected = 0; connection.Open(); IDbContextTransaction startedTransaction = null; try { if (connection.CurrentTransaction == null) { startedTransaction = connection.BeginTransaction(); } foreach (var commandbatch in commandBatches) { commandbatch.Execute(connection); rowsAffected += commandbatch.ModificationCommands.Count; } startedTransaction?.Commit(); } finally { startedTransaction?.Dispose(); connection.Close(); } return(rowsAffected); }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual async Task ExecuteNonQueryAsync( IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection, CancellationToken cancellationToken = default) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); var transactionScope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled); try { await connection.OpenAsync(cancellationToken); try { IDbContextTransaction transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed) { transaction = await connection.BeginTransactionAsync(cancellationToken); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); await transaction.DisposeAsync(); transaction = null; } await command.ExecuteNonQueryAsync(connection, cancellationToken : cancellationToken); } transaction?.Commit(); } finally { if (transaction != null) { await transaction.DisposeAsync(); } } } finally { await connection.CloseAsync(); } } finally { await transactionScope.DisposeAsyncIfAvailable(); } }
public override int SaveChanges(bool acceptAllChangesOnSuccess) { int rowsAffected; var timestamp = _systemClock.UtcNow; IDbContextTransaction transaction = null; if (Database.CurrentTransaction == null && Database.GetEnlistedTransaction() == null && Transaction.Current == null) { transaction = Database.BeginTransaction(); } using (transaction) { rowsAffected = base.SaveChanges(false); var changes = ChangeTracker.Entries() .Where(_ => _.ShouldAuditChanges()) .Select(entry => CreateChangeEvent(timestamp, entry)) .ToList(); ChangeTracker.AcceptAllChanges(); ChangeEvents.AddRange(changes); rowsAffected += base.SaveChanges(acceptAllChangesOnSuccess); transaction?.Commit(); } return(rowsAffected); }
public void CommitTransaction() { if (!transactionClosed) { transaction?.Commit(); transactionClosed = true; } }
public void Commit() { if (State == UnitOfWorkState.Entered) { _transaction?.Commit(); State = UnitOfWorkState.Committed; } }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual void ExecuteNonQuery( IEnumerable <MigrationCommand> migrationCommands, IRelationalConnection connection) { Check.NotNull(migrationCommands, nameof(migrationCommands)); Check.NotNull(connection, nameof(connection)); var userTransaction = connection.CurrentTransaction; if (userTransaction is not null && migrationCommands.Any(x => x.TransactionSuppressed)) { throw new NotSupportedException(RelationalStrings.TransactionSuppressedMigrationInUserTransaction); } using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) { connection.Open(); try { IDbContextTransaction?transaction = null; try { foreach (var command in migrationCommands) { if (transaction == null && !command.TransactionSuppressed && userTransaction is null) { transaction = connection.BeginTransaction(); } if (transaction != null && command.TransactionSuppressed) { transaction.Commit(); transaction.Dispose(); transaction = null; } command.ExecuteNonQuery(connection); } transaction?.Commit(); } finally { transaction?.Dispose(); } } finally { connection.Close(); } } }
internal static int CommitTransaction <TEntity>(DbContext context, IEnumerable <TEntity> entities, OperationType operation) where TEntity : class { // Retrieve entity type IEntityType entityType = context.Model.FindEntityType(typeof(TEntity)); EntityInfo entityMapping = new EntityInfo(entityType); // Sample random table looks like this [_VssBuildDefinition_02da3b] string randomTableName = $"[_{entityMapping.TableName}_{Guid.NewGuid().ToString().Substring(0, 6)}]"; // Build staging table sql command string stagingTableCommand = BulkOperationHelper.BuildStagingTableCommand(entityMapping, randomTableName, operation); // Open connection if it's closed if (context.Database.GetDbConnection().State == ConnectionState.Closed) { context.Database.GetDbConnection().Open(); } // If caller does not specify a transaction we create an internal transaction IDbContextTransaction internalTrasaction = null; if (context.Database.CurrentTransaction == null) { internalTrasaction = context.Database.BeginTransaction(); } // No entities to commit if (!entities.Any()) { return(0); } // Final step is to create staging table and merge data into final table try { // Create staging table and insert data into staging table BulkOperationHelper.ExecuteSqlCommandNonQuery(context, stagingTableCommand); BulkOperationHelper.BulkInsertToTable(entityMapping, context, entities.ToList(), randomTableName, operation); // Merge staging table data into final table and commit string mergeCommand = BulkOperationHelper.BuildMergeCommand(entityMapping, randomTableName, operation); int result = BulkOperationHelper.ExecuteSqlCommandNonQuery(context, mergeCommand); // Only commit internal transaction and let caller's transaction commit on their own internalTrasaction?.Commit(); // Return the number of records affected return(result); } catch (Exception) { // If anything goes wrong, we roll back to undo the operation internalTrasaction?.Rollback(); context.Database.CurrentTransaction?.GetDbTransaction().Rollback(); throw; } }
public int SaveChanges(IDbContextTransaction transaction = null) { var ret = base.SaveChanges(); if (ret > 0) { transaction?.Commit(); } return(ret); }
public Task <int> SaveAsync() { var count = _dbContext.SaveChangesAsync(); _transaction?.Commit(); _transaction?.Dispose(); _transaction = null; return(count); }
/// <summary> /// Saves changes in context (SaveChanges) and, if there is an open transaction, commits as well. /// </summary> /// <returns>True if there was any entity changed or false if there wasn't</returns> public bool Commit() { if (_context.SaveChanges() <= 0) { return(false); } _dbContextTransaction?.Commit(); return(true); }
private int Commit() { var result = 0; try { result = Context.SaveChanges(); Transaction?.Commit(); return(result); } catch (Exception e) { result = -1; Transaction?.Rollback(); Log.Error("Context Transaction Error"); Log.Error(e.Message); } return(result); }
public void Commit() { try { _dbTransaction?.Commit(); } finally { Dispose(); } }
public void CommitTransaction() { try { _currentTransaction?.Commit(); } catch { RollbackTransaction(); throw; } finally { if (_currentTransaction != null) { _currentTransaction.Dispose(); _currentTransaction = null; } } }
public void Commit() { if (_rollbackCount > 0) { _tran?.Rollback(); } else { _tran?.Commit(); } }
public void Commit() { if (dbContext != null) { dbContext.SaveChanges(); dbContextTransaction?.Commit(); } else { GetDbContextFromStack <DbContext>(false).SaveChanges(); } }
/// <summary> /// EFCore 提交 /// </summary> private int SaveChange() { int result; try { result = _dbUnitOfWork.SaveChanges(); _dbContextTransaction?.Commit(); } catch { _dbContextTransaction?.Rollback(); throw; } finally { _dbContextTransaction?.Dispose(); _dbUnitOfWork.Dispose(); } return(result); }
public void Commit() { try { _transaction?.Commit(); } catch (Exception ex) { throw; } }
public override void Commit() { if (rolledBack) { return; } db.SaveChangesAndDetach(); transaction?.Commit(); db.Dispose(); isComplete = true; }
public void Commit() { if (IsCommit) { return; } IsCommit = true; _dbContext.SaveChanges(); _dbContextTransaction?.Commit(); }
/// <inheritdoc /> public void OnActionExecuted(ActionExecutedContext context) { if (context.Exception == null || context.ExceptionHandled) { consentContext.SaveChanges(); transaction?.Commit(); } else { transaction?.Rollback(); } }
private void CommitAndSave() { try { SaveChanges(); currentTransaction?.Commit(); } catch { RollbackTransaction(); throw; } finally { if (currentTransaction != null) { currentTransaction.Dispose(); currentTransaction = null; } } }