Exemplo n.º 1
0
        public async Task <IDbContextTransaction> BeginTransactionAsync(params ICoreUnitOfWork[] coreUnitOfWorks)
        {
            DbContextTransaction = await Context.Database.BeginTransactionAsync();

            foreach (var coreUnitOfWork in coreUnitOfWorks)
            {
                coreUnitOfWork.Context.Database.UseTransaction(DbContextTransaction.GetDbTransaction());
            }
            return(DbContextTransaction);
        }
        private static SqlBulkCopy GetSqlBulkCopy(SqlConnection sqlConnection, DbContextTransaction transaction, bool keepIdentity = false)
        {
            if (transaction == null)
            {
                return(keepIdentity ? new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.KeepIdentity, null) : new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.CheckConstraints, null));
            }

            var sqlTransaction = (SqlTransaction)transaction.GetDbTransaction();

            return(keepIdentity ? new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.KeepIdentity, sqlTransaction) : new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.CheckConstraints, sqlTransaction));
        }
Exemplo n.º 3
0
    public async Task RollbackAsync(CancellationToken cancellationToken)
    {
        foreach (var dbContext in AttendedDbContexts)
        {
            if (dbContext.As <DbContext>().HasRelationalTransactionManager() &&
                dbContext.Database.GetDbConnection() == DbContextTransaction.GetDbTransaction().Connection)
            {
                continue; //Relational databases use the shared transaction if they are using the same connection
            }

            await dbContext.Database.RollbackTransactionAsync(CancellationTokenProvider.FallbackToProvider(cancellationToken));
        }

        await DbContextTransaction.RollbackAsync(CancellationTokenProvider.FallbackToProvider(cancellationToken));
    }