TransactionScope doesn't rollback transactions

59 views Asked by At

I guess I'm doing it all wrong but did not find practicle examples to prove the theory.

I have the following code

BusinessLayer

public static void AddEntities(ENTITY entity, int nb) {
    using(TransactionScope scope = new TransactionScope()) {
        for (int i = 0 ; i < nb ; i++) {
            DAL.AddEntity(entity);
        }
        scope.Complete();
    }
}

DataAccessLayer

public static void AddEntity(entity) {
    using(SqlCommand command = ...) {
        // code
        command.ExecuteNonQuery();
    }
}

If an exception is thrown and multiple entities have already been persisted, they are not rollback. But the rest is not persisted also. I need everything to get persisted or nothing.

0

There are 0 answers