title: Entity Framework Core : The database transaction is not always rolled back on errors Entity Framework
body: <p>An action item from the security scan is to address the following error</p>
  <blockquote>
  <p>The database transaction is not always rolled back on errors entity framework in DatabaseContext.cs</p>
  </blockquote>
  <ol>
  <li>Open database connection with try {} block</li>
  <li>Begin database transaction with try {} block</li>
  <li>No unfinished transaction before closing database</li>
  <li>Ensure the transaction is rolled back in the catch { } block or finally { } block</li>
  </ol>
  <p>The method that causes this issue is</p>
  <pre><code>    public async Task CommitTransactionAsync(IDbContextTransaction transaction)
      {
  
          if (transaction == null)
          {
              throw new ArgumentNullException(nameof(transaction));
          }
  
          try
          {
              await this.SaveChangesAsync().ConfigureAwait(false);
              transaction.Commit();
          }
          catch
          {
              this.RollbackTransaction();
              throw;
          }
          finally
          {
              if (this.currentTransaction != null)
              {
                  this.currentTransaction.Dispose();
                  this.currentTransaction = null;
              }
          }
  
      }
  </code></pre>
  <p>As far as I see, Catch block handles the roll back in case of any errors. Am I missing anything here?</p>
  <p><strong>Update:</strong></p>
  <p>Below is BeginTransaction method</p>
  <pre><code>    public async Task&lt;IDbContextTransaction&gt; BeginTransactionAsync()
      {
          if (this.currentTransaction != null)
          {
              return null;
          }
  
          this.currentTransaction = await this.Database.BeginTransactionAsync(IsolationLevel.ReadCommitted).ConfigureAwait(false);
  
          return this.currentTransaction;
      }
  </code></pre>
  <p>and it is used like</p>
  <pre><code>using (var transaction = await this.demoRepository.UnitOfWork.BeginTransactionAsync().ConfigureAwait(false))
                  {
  await this.demoRepository.UnitOfWork.CommitTransactionAsync(transaction).ConfigureAwait(false);
                  }
  </code></pre>