Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.

What I have tried:

I have tried to find it on google but no success.
Posted
Updated 15-Jul-16 0:06am

That would probably be because Entity Framework automatically wraps all update, insert and delete operations in a transaction by default. You don't have to create your own transaction context.

Read more on it here[^].
 
Share this answer
 
v2
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
C#
here

.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900