Entity Framework: Is it possible to use transactions and rollbacks?

entity-framework transactions

Question

Issue: (With Sql 2005)

  • How can I run a database query while a transaction is active? (Because it secures the table)
  • How can a transaction be made to roll back before closing so that the table may be queried?

So here's what I discovered:

[TestMethod]
public void CreateUser()
{
    TransactionScope transactionScope = new TransactionScope();

    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();

    DataContextHandler.Context.Dispose();
}

Where The context object for my entities is exposed via DataContextHandler, a straightforward singleton. This seems to operate as you would anticipate. The user is created, saved, and then rolled back after the application is finished. (IE test is complete)

Problem: In order to query the database, I need to force the transaction to rollback and kill itself.

Reason: In order to do testing, I need to confirm that the user:

  • Gets saved
  • may be accurately questioned to demonstrate its existence.
  • the removal (Junk data)
  • Can be checked to see whether it was taken out.

Currently, the only way I can get the transaction to rollback is if the test is successful AND I'm unable to figure out how to query while the transaction is active:

[TestMethod]
public void CreateUser()
{
    ForumUser userToTest = new ForumUser();

    TransactionScope transactionScope = new TransactionScope();

    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();     

    Assert.IsTrue(userToTest.UserID > 0);

    var foundUser = (from user in DataContextHandler.Context.ForumUser
                    where user.UserID == userToTest.UserID
                    select user).Count();  //KABOOM Can't query since the 
                                           //transaction has the table locked.

    Assert.IsTrue(foundUser == 1);

    DataContextHandler.Context.Dispose();

    var after = (from user in DataContextHandler.Context.ForumUser
                 where user.UserID == userToTest.UserID
                 select user).Count(); //KABOOM Can't query since the 
                                       //transaction has the table locked.

    Assert.IsTrue(after == 0);
}

UPDATE Rolling back and checking works thanks to this, however there is still no way to query inside the using part.

using(TransactionScope transactionScope = new TransactionScope())
{
    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();
    Assert.IsTrue(userToTest.UserID > 0);
    //Still can't query here.

}

var after = (from user in DataContextHandler.Context.ForumUser
            where user.UserID == userToTest.UserID
            select user).Count();

Assert.IsTrue(after == 0);
1
18
1/16/2009 9:58:13 PM

Popular Answer

from 5 to Zzz;

"SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted. "

Therefore, it would seem that you do not need to explicitly integrate your own transaction processing viaTransactionScope .

15
2/24/2014 1:06:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow