1

在一个函数中,我传递相关值的列表并循环它们以将更改保存在数据库中。一切正常,直到 2 个人或更多人使用同一页面更新不同的实体。然后只保存最近进行更改的数据。

   public bool UpdatePermanentDifferenceBL(List<PermanentDifferenceProperties>                                permDiffDetails)
   {
       try
       {
           int permanentDifferenceID=0;
           int taxEntityID = 0;
           int mapid = 0;

           //using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
           //{


                  Topaz.DAL.PermanentDifference permDiff;


                      for (int i = 0; i < permDiffDetails.Count; i++)
                      {

                          if ((bool)(HttpContext.Current.Session[GlobalConstant.currentDataSet]) == true && (int)(HttpContext.Current.Session[GlobalConstant.snapShotID]) == 0)
                          {
                              using (var ctx = new TopazDbContainer())
                              {

                                   try
                                 {
                                      permanentDifferenceID = permDiffDetails[i].PermanentDifferenceID;
                                      taxEntityID = permDiffDetails[i].TaxEntityID;
                                      mapid = permDiffDetails[i].MapID;

                                      permDiff = new Topaz.DAL.PermanentDifference();


                                      permDiff = ctx.PermanentDifference.Where(p => p.PermanentDifferenceID == permanentDifferenceID && p.TaxEntityID == taxEntityID && p.MapID == mapid).SingleOrDefault();
                                      permDiff.Business = permDiffDetails[i].Business;
                                      permDiff.Interest = permDiffDetails[i].Interest;
                                      permDiff.Corporate = permDiffDetails[i].Corporate;
                                      permDiff.UnallocatedTax = permDiffDetails[i].UnallocatedTax;
                                      permDiff.Total = permDiffDetails[i].Total;
                                      permDiff.ModifiedBy = permDiffDetails[i].ModifiedBy;
                                      permDiff.ModifiedDate = DateTime.Now;
                                      ctx.SaveChanges();

                              }
                           catch (System.Data.OptimisticConcurrencyException ex)
                              {
                                    permanentDifferenceID = permDiffDetails[i].PermanentDifferenceID;
                                    taxEntityID = permDiffDetails[i].TaxEntityID;
                                    mapid = permDiffDetails[i].MapID;
                                    permDiff = new Topaz.DAL.PermanentDifference();                                  
                                    ctx.Refresh(System.Data.Objects.RefreshMode.StoreWins, permDiff);
                                    permDiff = ctx.PermanentDifference.Where(p => p.PermanentDifferenceID == permanentDifferenceID && p.TaxEntityID == taxEntityID && p.MapID == mapid).SingleOrDefault();
                                    permDiff.Business = permDiffDetails[i].Business;
                                    permDiff.Interest = permDiffDetails[i].Interest;
                                    permDiff.Corporate = permDiffDetails[i].Corporate;
                                    permDiff.UnallocatedTax = permDiffDetails[i].UnallocatedTax;
                                    permDiff.Total = permDiffDetails[i].Total;
                                    permDiff.ModifiedBy = permDiffDetails[i].ModifiedBy;
                                    permDiff.ModifiedDate = DateTime.Now;
                                    ctx.SaveChanges();
                                  }


                              }
                      }


                      //ctx.ContextOptions.UseLegacyPreserveChangesBehavior = true;



                  }



               //}


           //using (UnitOfWork uow = new UnitOfWork())
           //{
           //    for (int i = 0; i < permDiffDetails.Count; i++)
           //    {
           //        if ((bool)(HttpContext.Current.Session[GlobalConstant.currentDataSet]) == true && (int)(HttpContext.Current.Session[GlobalConstant.snapShotID]) == 0)
           //        {
           //            Repository.PermanentDifferenceRepository pDiffRepo = new Repository.PermanentDifferenceRepository(uow);
           //            Topaz.DAL.PermanentDifference permDiff = pDiffRepo.GetByEntityId(permDiffDetails[i].PermanentDifferenceID, permDiffDetails[i].TaxEntityID, permDiffDetails[i].MapID);


           //            permDiff.Business = permDiffDetails[i].Business;
           //            permDiff.Interest = permDiffDetails[i].Interest;
           //            permDiff.Corporate = permDiffDetails[i].Corporate;
           //            permDiff.UnallocatedTax = permDiffDetails[i].UnallocatedTax;
           //            permDiff.Total = permDiffDetails[i].Total;
           //            permDiff.ModifiedBy = permDiffDetails[i].ModifiedBy;
           //            permDiff.ModifiedDate = DateTime.Now;
           //            pDiffRepo.ApplyChanges(permDiff);

           //        }

           //        else
           //        {
           //            int snapshotID = (int)(HttpContext.Current.Session[GlobalConstant.snapShotID]);
           //            SnapShotPermanentDifferenceRepository pDiffRepo = new SnapShotPermanentDifferenceRepository(uow);
           //            SnapShotPermanentDifference permDiff = pDiffRepo.GetByEntityId(permDiffDetails[i].PermanentDifferenceID, permDiffDetails[i].TaxEntityID, permDiffDetails[i].MapID,snapshotID);

           //            permDiff.SnapshotID = snapshotID;
           //            permDiff.Business = permDiffDetails[i].Business;
           //            permDiff.Interest = permDiffDetails[i].Interest;
           //            permDiff.Corporate = permDiffDetails[i].Corporate;
           //            permDiff.UnallocatedTax = permDiffDetails[i].UnallocatedTax;
           //            permDiff.Total = permDiffDetails[i].Total;
           //            permDiff.ModifiedBy = permDiffDetails[i].ModifiedBy;
           //            permDiff.ModifiedDate = DateTime.Now;
           //            pDiffRepo.ApplyChanges(permDiff);


           //        }
           //    }
           //    uow.SaveChanges();
               return true;
           }
       catch (Exception ex)
       {
           TopazErrorLogs.AddTopazErrorLogBL(ex, 1, 1);
           throw new TopazCustomException(GlobalConstant.errorMessage);

       }

   }

非常感谢任何紧急援助。

4

2 回答 2

1

解决此问题的常用方法是向要处理并发的每个表添加额外的列。此列将具有ROWVERSIONTIMESTAMP数据类型。在 EDMX 中映射表后,您将获得一个新的计算类型属性byte[]并将其并发模式设置为固定。此设置将强制 EF 向每个 UPDATE 或 DELETE 语句添加额外的 WHERE 条件,以验证该行版本与从数据库加载记录时的版本相同。如果行版本不同(另一个线程更新了记录),则找不到修改的记录,您将获得异常。

每次您自动更新记录时,数据库都会更改该值。您只需要确保您的实体使用从数据库加载实体时检索到的值。

于 2012-04-17T08:38:58.437 回答
0

道歉所有的视图状态,在某种程度上,会话是罪魁祸首,而不是 EntityFramework。

于 2012-04-21T20:13:16.647 回答