none
How to implement transaction management using multiple DbContexts RRS feed

  • Question

  • Hi All,

    I have a doubt. Suppose I have more than one DbContext objects/contexts say ObjContext1 and ObjContext2. Through ObjContext1 I am updating or performing any operation on Table1 and with ObjContext2 I am updating/performing any operation on Table2.

    How can I implement transaction management in this scenario? I mean suppose when I am updating Table1 (by ObjContext1 ) this is successful. But Updating Table2 (by ObjContext2) is unsuccessful then I want even the update to Table1 to be rolled back through Transaction Management. How can I achieve this?

    Please help through a simple example or a small piece of code.

    Thanks in advance. 

    Thursday, March 22, 2012 8:32 AM

Answers

  • Hi Vaibhav.1801sh,

    Welcome to MSDN Forum.

    We can use TransactionScope to realize it. This class can make a code block transactional. Below is a demo.

    using (TransactionScope scope = new TransactionScope())  
               {  
                   using (myContext context = new myContext())  
                  {  
                      Test t = new Test()  
                      t.Name="TEST";
                      context.AddToTests(t);  
                      context.SaveChanges();  
                   }  
      
                   using (myContext context = new myContext())  
                   {  
                         
                       Demo d = context.Demos.First(s => s.Name == "Demo1");  
                       context.DeleteObject(d);  
                       context.SaveChanges();  
                   }  
                   scope.Complete();  
               }  

    More information about this class, please refer to the link.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 5:43 AM
    Moderator

All replies

  • Hi Vaibhav.1801sh,

    Welcome to MSDN Forum.

    We can use TransactionScope to realize it. This class can make a code block transactional. Below is a demo.

    using (TransactionScope scope = new TransactionScope())  
               {  
                   using (myContext context = new myContext())  
                  {  
                      Test t = new Test()  
                      t.Name="TEST";
                      context.AddToTests(t);  
                      context.SaveChanges();  
                   }  
      
                   using (myContext context = new myContext())  
                   {  
                         
                       Demo d = context.Demos.First(s => s.Name == "Demo1");  
                       context.DeleteObject(d);  
                       context.SaveChanges();  
                   }  
                   scope.Complete();  
               }  

    More information about this class, please refer to the link.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 5:43 AM
    Moderator
  • Thanks a lot Allen.

    I got it.

    Tuesday, March 27, 2012 1:54 PM