Share via

Execution Timeout Expired In MVC5

AHSAN ALI 101 Reputation points
2023-10-21T08:47:49.6266667+00:00

i am getting this error when is use more then two operation in transaction scope. When i run this code without transaction its working fine .in transaction its gives error in _bookingViasServices.Save method _unitOfWork.SaveChanges();

public bool Edit(Booking booking, List<BookingVia> listOfBookingVias)
        {
            _unitOfWork.BeginTransaction();
            try
            {
                _bookingRepository.Update(booking);
               
                var lastInsertedRecordID = booking.ID;

                foreach (var itemVia in listOfBookingVias)
                {
                    itemVia.BookingID = lastInsertedRecordID;
                }
               var isAllRecordDeleted= _bookingViasSerivces.DeleteViasByBookingId(lastInsertedRecordID);
                _unitOfWork.SaveChanges();
                if (isAllRecordDeleted)
                {
                   
                    var isSaved= _bookingViasSerivces.Save(listOfBookingVias); 


                    if(isSaved)
                    {
                        _unitOfWork.Commit();
                        return true;
                    }
                    else
                    {
                        _unitOfWork.Rollback();
                        return false;
                    }
                   
                }
                else
                {
                   // _unitOfWork.Rollback();
                    return false;
                }
               

               
            }
            catch (Exception)
            {
                _unitOfWork.Rollback();
                return false;
            }
        }


this is bookingViasServices save methode

 public bool Save(List<BookingVia> listOfBookingVias)
        {
            try
            {
                _bookingViaRepository.AddRange(listOfBookingVias);
                _unitOfWork.SaveChanges();  
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
Developer technologies | ASP.NET Core | Other

Answer accepted by question author

Lan Huang-MSFT 30,221 Reputation points Microsoft External Staff
2023-10-26T02:35:18.0833333+00:00

Hi @AHSAN ALI,

You were using Database.BeginTransaction() before. While Database. BeginTransaction() is used only for database related operations transaction, System.Transactions.TransactionScope for mixing db operations and C# code together in a transaction.

From MSDN:

The System.Transactions infrastructure provides both an explicit programming model based on the Transaction class, as well as an implicit programming model using the TransactionScope class, in which transactions are automatically managed by the infrastructure.

The TransactionScope class provides a simple way to mark a block of code as participating in a transaction, without requiring you to interact with the transaction itself.

Useful links:

Database.BeginTransaction vs Transactions.TransactionScope

Implementing an Implicit Transaction using Transaction Scope

Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.