0

My question is toward the following test:

var connectionString = "Data Source=.\\sqlexpress;Initial Catalog=Axineh;Integrated Security=True";

using (var scope = new TransactionScope())
{
    var dbContext = new AppDbContext(connectionString);

    // first transaction, save a new Paper
    var t1 = dbContext.Database.BeginTransaction();

    var paper = new Paper {EnglishName = "Paper", Name = "Paper"};
    dbContext.Set<Paper>().Add(paper);
    dbContext.SaveChanges();
    var paperId = paper.PaperId;

    t1.Commit();

    var p1 = dbContext.Set<Paper>().Find(paperId); // p1 = paper

    // second transaction, does nothing but calling rollback
    var t2 = dbContext.Database.BeginTransaction();
    t2.Rollback();

    var p2 = dbContext.Set<Paper>().Find(paperId); // p2 = null

}

Assert.NotNull(p1);
Assert.Null(p2);

As you can see I'm using EF Transaction within a TransactionScope. There are two EF transactions on the test t1 and t2. t1 (The first one) saves a new Paper entity and commit it's changes then t2 (the second one) does nothing but calling the Rollback method. But after the rollback method is called, The created Paper on t1 also rollsback. Though it has already been committed. I don't the reason why this happens and how to get this task done.

Please consider that I need the TransactionScope for my Integration Tests in order to keep the database clean. So any alternative solution to cleanup the db after the test is done would also be as helpful.

Hossein Shahdoost
  • 1,692
  • 18
  • 32
  • Either all transactions in the scope are committed, or all of them are rolled back. Is this a bug? – Dennis Apr 04 '16 at 07:30
  • @Dennis yeah, I shouldn't have had called it a bug, But this is a problem I'm facing trying to test Transactions. Is there any workaround? – Hossein Shahdoost Apr 04 '16 at 07:34
  • Just some things to check. Does EF support nested transactions. When you commit, query your db and see if the data is there cause it may only be kept in context cache. See if the data is in your db there after disposing the db context. These should give you clues on how to proceed. For example in your test I'd try using a different db context to read the data as that will show it is really committed. – Daniel van Heerden Apr 04 '16 at 07:58
  • @DanielvanHeerden It does not commit the changes to database (though using a different db context wouldn't show this thanx to DTC), But should TransactionScope be effected by the EfTransactions? is there anyway to separate them in this context? – Hossein Shahdoost Apr 04 '16 at 09:53
  • 1
    I did a quick google and couldn't find reference to an EfTransaction. The dbContext is a transient cache, not a transaction as I understand it. So it shouldn't affect the TransactionScope, which is a C# wrapping of the actual SQL transaction. – Daniel van Heerden Apr 04 '16 at 10:15
  • @DanielvanHeerden I think BeginTransaction unlike dbContext refers to an actual SQL transaction and It's possible that they have conflictions. but this behavior still looks strange to me. – Hossein Shahdoost Apr 04 '16 at 10:32
  • http://stackoverflow.com/questions/22382892/database-begintransaction-vs-transactions-transactionscope – Daniel van Heerden Apr 04 '16 at 10:55

0 Answers0