簡體   English   中英

事務未在實體框架中得到提交

[英]transaction is not getting commit in entity framework

嘗試執行復雜的過程,並使用實體框架將數據保存在事務表中。 當我的代碼提交事務時,它將引發異常

無法執行事務操作,因為有正在處理的未決請求。

下面是代碼。

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();
            }
        }

不知道為什么它會引發掛起的請求異常,因為我正在同一事務中執行它。

以下是完整的堆棧跟蹤

   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

我找到了另一種方法。

而不是直接從dbcontext調用存儲過程,而是像dbContext.Database.DbRawSqlQuery那樣調用它。 現在我按以下順序做所有上述事情

  1. 聲明和初始化dbcontext。
  2. 創建交易。
  3. 使用在步驟1中初始化的上下文執行過程。
  4. 使用相同的ddContex將記錄保存在databae中。
  5. 將更改從dbContext保存到數據庫。
  6. 提交交易。

甚至不需要創建事務,因為默認情況下,所有與數據庫相關的實體語句都在事務中執行。 可以在這里閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM