0

The code below throws the following exception. What am I doing wrong?

Error Stack: System.Transactions.TransactionAbortedException: The transaction has aborted. ---> System.Data.SqlClient.SqlException: The transaction operation cannot be performed because there are pending requests working on this transaction.

public class NestedTransaction
{
    public void SomeTransaction()
    {
        using (var context = new SomeDBContext())
        {
            using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required,
                  new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
             {
                context.Connection.Open();

                List<SomeEntities> entities = GetEntitiesFromDatabase();

                    entities.ForEach(entity =>
                    {
                        SaveEntity((EntityConnection)context.Connection, entity);                            
                    });

            context.SaveChanges();

            context.Connection.Close();

            transaction.Complete();

            }               
        }
    }

    private static void SaveEntity(EntityConnection connection,SomeEntity entity)
    {
        foreach (SomeEntity childEntity in entity.ChildEntities)
        {
              SaveEntity(connection, childEntity)
        }
    }
}

Thanks in advance Niju

niju
  • 117
  • 1
  • 8

0 Answers0