locked
How to use Transaction Scope in EF RRS feed

  • Question

  • User753996341 posted

    Hi!

    I am using same mydbcontext and i have multiple inserts in multiple methods and in all the method i am passing the main method's mydbcontext and saving the data in each method and finally I am saving the main method. So I wanted to know if I use one Transaction scope in the main method is it sufficient.

    public Order Save(string Data,string Data1,string Data2){
    
      Order Order = new Order();
      try {
            using (var transaction = new System.Transactions.TransactionScope()) 
            {
                  MyDBContext MyDBContext = new MyDBContext();
                  Order = Save(param, MyDBContext);
    
    
                  SaveOrderData(MyDBContext , Data)
    
                 SaveOrderPayment(MyDBContext , Data1)
    
                 SaveOrderInvoice(MyDBContext , Data2)
    
    
                 transaction.Complete();
             }
         }
         catch (System.Transactions.TransactionException ex) {
              Log(ex.Message);
         }
         catch (System.ApplicationException ex) {
            Log(ex.Message);
            throw;
        }
       return Order;
    
    }


     

    Friday, November 18, 2016 10:21 AM

All replies

  • User-1877975950 posted

    Hi malik,

    what I do is to invalidate 1 and  then more of the transactions and see how the transaction rolls back, just to make certain the rollback is acting how I expect.

    With respect, I wouldn't leave it to being advised by someone on a forum. Maybe I am a bit too careful.

    For a start, I assume you are using MS SQL SERVER. There are other assumptions too.

    Best regards

    Stew

    Friday, November 18, 2016 11:37 AM
  • User753996341 posted

    Hi!

    Thanks for you kind reply

    No, I am using MYSQL.

    If my nested 1 method save and other method failed to save. still first method got save why??

    Monday, November 28, 2016 10:31 AM
  • User-271186128 posted

    Hi olemalik,

    How to use Transaction Scope in EF

    As for this issue, I suggest you could refer to the following code to use Transaction:

    using (System.Data.Entity.DbContextTransaction dbTran = context.Database.BeginTransaction( ))
        {
            try
            {
                Student std1 = new Student() { StudentName = "newstudent" };
                context.Students.Add(std1);
                context.Database.ExecuteSqlCommand(
                    @"UPDATE Student SET StudentName = 'Edited Student Name'" +
                        " WHERE StudentID =1"
                    );
                context.Students.Remove(std1);
    
                //saves all above operations within one transaction
                context.SaveChanges();
    
                //commit transaction
                dbTran.Commit();
            }
            catch (Exception ex)
            {
                //Rollback transaction if exception occurs
                dbTran.Rollback();
            }
    
        }

    More details, see:

    http://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx

    https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

    Best regards,
    Dillion

    Friday, December 2, 2016 8:46 AM