lundi 18 avril 2016

How to read data after rolling back nested transaction c# .net, EF 6 Code First

I'm trying to test data modification scenario, that works like that:

TestMethod()
{
    var repository = new Repository(); // create DbContext
    var service = new Service(repository); // create service using DbContext
    using(var scopeOuter = new TransactionScope()) // enter outer tansaction
    {
        var testData = service.AddData() // invoke method that creates some data, it will succed

        service.DoRoutine(testData); // DoRoutine is some complicated logic that might fail, and in test scenario will

        var data = repository.GetTestData(testData.id) // get entities created by AddData

        Assert.TestDataIsCorrect(data) // check that entity is in correct state
    }
}

// Do routine looks something like this:
Service.DoRoutine(data)
{
    using(var scope new TransactionScope())
        SomeComplicatedLogic(data);
}

Code written this way will fail on repository.GetTestData(id) with exception "The operation is not valid for the state of the transaction."

What I'm trying to do here, is to check that "Some complicated logic" will rollback properly after failure, to be sure that i have everything covered and properly set up inside transaction scopes, and that DoRoutine will leave no side effects on test data after rolling back.

I've tried to research how to continue reading data after transaction rollback, but all sources i found say that there are no nested transactions, and inner transaction scope either binds to outer scope or creates new independent transaction.

Is there way to do this using C#, EF 6.0 and MsSQL server ? Or maybe there is something wrong with desing of code / test ?

Hope to get some advice on that,

Regards

Aucun commentaire:

Enregistrer un commentaire