I have began using TransactionScope to assist with my unit tests, to be able to put my test database to it's previous condition. By using this with SpecFlow, I've got a base class like so:

public class TransactionScopedFeature
{
    private TransactionScope Scope { get; set; }

    [BeforeScenario]
    public void BaseSetup()
    {
        this.Scope = new TransactionScope(TransactionScopeOption.RequiresNew);
    }

    [AfterScenario]
    public void BaseCleanup()
    {
        if (this.Scope != null)
        {
            this.Scope.Dispose();
        }
    }
}

The suggestions above works, for the reason that after i add records towards the database, after i then query the tables following the tests have completely finished, individuals tables are empty. Great stuff, and incredibly clever indeed!

My question pertains to identity posts during these tables. What I have observed is the fact that, after i run my tests numerous occasions, the ID column of my test table increases by 1. I believed that because the TransactionScope would rollback the alterations, the identity seed would be also folded back.

Shall We Be Held wrong for making this assumption - is precisely how databases work? If that's the situation, I possibly could also operate a SQL script before all of my situations that performs this:

DBCC CHECKIDENT ('dbo.Items', reseed, 0)

I simply desired to sign in situation I had been doing a problem, or this really is normal database behavior.

Cheers. Jas.

The seed value to have an identity column doesn't get folded back using the relaxation of the transaction in SQL Server.

This really is by design to ensure that a unique lock doesn't have to become positioned on the counter for that identity for the whole amount of the transaction.