Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I was wondering is there any difference:

Version 1:
C#
using (dbcontext = new DBContext())
{
   //read from context here
   //modify context here
   using (transactionscope = new TransactionScope())
   {
      dbcontext.SaveChanges();

      dbcontext.Database.ExecuteSQLCommand("UPDATE table...");
      transactionscope.Complete();
   }
}

Version 2:
C#
using (transactionscope = new TransactionScope())
{
   using (dbcontext = new DBContext())
   {
      //read from context here
      //modify context here
      dbcontext.SaveChanges();

      dbcontext.Database.ExecuteSQLCommand("UPDATE table...");
   }
   transactionscope.Complete();
}

Thanks
Posted
Updated 27-Sep-15 21:11pm
v4

1 solution

First thing is that the SaveChanges does not need a separate transaction since it's wrapped inside a transaction by default.

The bigger question is what other operations you do with the context. For example does the modify context mean another call to SaveChanges. If it does, then wrapping both SaveChanges calls inside a single transaction ensures that all or nothing is saved.

Another scenario would be if you use two contexts. Again it would make sense to wrap them inside a single transaction.
 
Share this answer
 
Comments
Jacob_Zac 28-Sep-15 3:07am    
By additional context updates I was thinking of using DBContext.Database.ExecuteSQLCommand and doing updates to multiple rows.
Wendelius 28-Sep-15 9:46am    
That's what I suspected. In your version 1 those operations would be outside the scope of a transaction so you would not have a single unit of work.

Because of this I'd always prefer using the transaction scope as the outermost element.
Jacob_Zac 28-Sep-15 10:57am    
Thank you. I don't understand why would any of those operations be outside of scope of transaction. Both SaveChanges and ExecuteSQLCommand are inside of using (transactionscope = new TranasctionScope()){} in version 1.
Wendelius 29-Sep-15 13:54pm    
If all the operations are inside the transactionscope then they are part of the transaction.

Since the original code looked like this

using (dbcontext = new DBContext())
{
//read from context here
//modify context here
using (transactionscope = new TransactionScope())
{
dbcontext.SaveChanges();

//some additional context updates
transactionscope.Complete();
}

I thought that part of the modifications were done before entering the transaction. But as said if they are inside transaction, they are safe :)

Either way I'd still prefer setting the transaction on outermost level.
Jacob_Zac 29-Sep-15 15:57pm    
Thank you Mika for all the info. I am sorry that my original code (the one without ExecuteSQlCommand) was not clear enough.
Basically I just wanted to be sure that by modifying context outside of transaction scope and then saving it inside of the scope would work properly. So just to be clear is that safe? Again thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900