title: How to unit-test transactions in Entity Framework Core?
body: <p>I have a method that does some work in a transaction:</p>
  
  <pre><code>public async Task&lt;int&gt; AddAsync(Item item)
  {
      int result;
  
      using (var transaction = await _context.Database.BeginTransactionAsync())
      {
          _context.Add(item);
  
          // Save the item so it has an ItemId
          result = await _context.SaveChangesAsync();
  
          // perform some actions using that new item's ItemId
          _otherRepository.Execute(item.ItemId);
  
          transaction.Commit();
      }
  
      return result;
  }
  </code></pre>
  
  <p>I'd like to add unit tests to check that if <code>_context.SaveChangesAsync</code> or <code>_otherRepository.Execute</code> fail then the transaction is rolled back, is that possible? </p>
  
  <p>I can't see a way to do that using <a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory" rel="nofollow noreferrer">InMemory</a> or <a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite" rel="nofollow noreferrer">SQLite</a>?</p>