Send mail to the author(s) E-mail

# Wednesday, 27 April 2016

Inspired by:

which was itself inspired by a Jimmy Bogard article

If you get the MSDTC error

Here is the start of an example just using rollback transactions

[TestClass] public class TransactionTests { [TestMethod] public void Insert_User_and_test_transaction_rolls_back() { var user = new User { CreatedDate = DateTime.Now, DisplayName = "Todd Smith4", Email = "", Active = true }; var sql = "INSERT INTO dbo.Users (CreatedDate, DisplayName, Email, Active) VALUES (@CreatedDate, @DisplayName, @Email, @Active)"; db.Execute(sql, user); var retrievedUser = db.Query<User>("select * from users where Email = @email", new { email = user.Email }).FirstOrDefault(); Assert.AreEqual(user.DisplayName, retrievedUser.DisplayName); Assert.AreEqual(user.Active, retrievedUser.Active); Assert.AreEqual(user.CreatedDate.ToString(), retrievedUser.CreatedDate.ToString()); }


private TransactionScope scope; private DbConnection db; private DapperUserRepositoryTestContext c; private bool rollBackTransaction = true; // before each test [TestInitialize] public void SetUp() { // direct access to the db db = new SqlConnection( ConfigurationManager.ConnectionStrings["DapperMangoTestingConnectionString"].ConnectionString); if (rollBackTransaction) scope = new TransactionScope(); // going through the repo c = new DapperUserRepositoryTestContext(); } // after each test [TestCleanup] public void TearDown() { if (rollBackTransaction) scope.Dispose(); db.Dispose(); }