Why does DbContext in EF Core return changed entity after Transaction Rollback

c# entity-framework entity-framework-core

Question

I want to verify that Transaction is rolled back on the case of an exception. The below method can potentially throw an exception.

// method Checkout(user, reduceMoney, addBill)
using (var transaction = _botContext.Database.BeginTransaction())
{
    try
    {
        await FirstDbUpdate(user, reduceMoney);
        await SecondDbUpdate(user, addBill); // potential exception

        transaction.Commit();
    }
    catch (Exception ex)
    {
        // ...
    }
}

To verify the rollback I wrote this test:

[Test]
public async Task Test_TransactionFail_RevertedUserUpdate()
{
    // Arrange user{budget: 10}
    var user = await _userRepository.GetByName("John Doe");
    var reduceMoney = 10m;

    // Act - null causes an exception
    await _service.Checkout(user, reduceMoney, null);  

    // Assert
    var userRetrieved = await _userRepository.GetByName("John Doe");
    Assert.AreEqual(10, userRetrieved.Budget);
}

Assertion fails because the user has a budget of 0. i.e. it looks like the change was persisted, but in reality, it is not (which is the correct behavior).

So the question is why does DbContext return the not persisted data, and where does it get it from? Shouldn't the rollback have removed them?

1
2
9/9/2018 11:20:30 PM

Accepted Answer

Because you are in the some context instance, the query in Assert will load from the same instance instead to have a direct query to your database.

Try with other instance or reload it https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.changetracking.entityentry.reload?view=efcore-2.0

1
9/11/2018 12:59:50 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow