transaction is not getting commit in entity framework

c# entity-framework-6

Question

Trying to execute complex procedure and saving a data in table in transaction using entity framework. When my code commits the transaction then it is throwing exception

The transaction operation cannot be performed because there are pending requests working on this transaction.

Below is the code.

using (abcEntities dbContext = new abcEntities())
        {
            using (var transaction = dbContext.Database.BeginTransaction())
            {
                dbContext.USP_ResellerCustomerFile_DeleteFiles(customerId, fileId, 1);
                ResellerAndCustomerActivityDAL objCustomerActivityDAL = new ResellerAndCustomerActivityDAL();

                ResellerAndCustomerActivity objActivity = new ResellerAndCustomerActivity()
                {
                    Activity = objfile.FileName + " is temporairly permanently deleted.",//message.ReplacingSpecialCharacterswithEntities(),
                    ActivityDate = DateTime.UtcNow,
                    ResellerAndCustomerId = customerId,
                    UserTypeId = 2

                };

                dbContext.ResellerAndCustomerActivities.Add(objActivity);

                //objCustomerActivityDAL.Create(objfile.FileName + " is permanently deleted.", objUnit, out returnMessage, customerId);

                dbContext.SaveChanges();
                transaction.Commit();
            }
        }

Don't know why it is throwing pending request exception because I am executing it in same transaction.

Below is complete stack trace

   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbTransactionDispatcher.Commit(DbTransaction transaction, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.EntityTransaction.Commit()
   at System.Data.Entity.DbContextTransaction.Commit()
   at BoomerangResellerAPI.DAL.ResellerCustomerFileDAL.DeleteFile(Int32 customerId, Int32 fileId, String& returnMessage) in D:\Varinder\Working Folder\Boomerang Backup\API\BoomerangResellerAPI\BoomerangResellerAPI\BoomerangResellerAPI.DAL\ResellerCustomerFileDAL.cs:line 325
   at BoomerangResellerAPI.BLL.ResellerCustomerFileBLL.DeleteFile(Guid uniqueCustomerId, Int32 fileId, String& returnMessage) in D:\Varinder\Working Folder\Boomerang Backup\API\BoomerangResellerAPI\BoomerangResellerAPI\BoomerangResellerAPI.BLL\ResellerCustomerFileBLL.cs:line 100
   at BoomerangResellerAPI.Controllers.FilesController.TemporaryDelete(Nullable`1 customerUniqueId, Nullable`1 fileId) in D:\Varinder\Working Folder\Boomerang Backup\API\BoomerangResellerAPI\BoomerangResellerAPI\BoomerangResellerAPI\Controllers\FilesController.cs:line 195
1
2
7/15/2016 10:43:30 AM

Popular Answer

I found an alternate way.

Rather then calling Stored Procedure from dbcontext directly I am calling it like dbContext.Database.DbRawSqlQuery. Now I am doing all the above thing in following order

  1. Declaring and initializing dbcontext.
  2. Creating transaction.
  3. Executing procedure using context initialized in step 1.
  4. Saving record in databae using same ddContex.
  5. saving changes from dbContext to database.
  6. Committing transaction.

Even transaction is not needed to create because all the entity(related to database) statements are executed in transaction by default. It can be read here

0
7/15/2016 10:02:49 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow