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.