Count不考虑新插入的记录

时间:2019-11-06 10:07:46

标签: c# linq

插入第一条记录后,即使插入后,计数仍显示为0。我可以看到SaveContext()插入的记录 被执行。因此,看起来userChangeRequestApprovalRepository没有用新插入的数据刷新。 像下面的语句那样计数+1是否合适

userChangeRequestApprovalRepository.Where(x => x.UserChangeRequestId == userChangeRequest.Id).Count() + 1; 

代码

 InsertUserChangeRequestApproval(userChangeRequest);
  SaveContext();

 var numberOfAprovals = userChangeRequestApprovalRepository.Where(x => x.UserChangeRequestId == userChangeRequest.Id).Count();

插入方法

    private void InsertUserChangeRequestApproval(UserChangeRequest userChangeRequest)
            {
                UserChangeRequestApproval userChangeRequestApproval = new UserChangeRequestApproval()
                {
                    UserChangeRequestId = userChangeRequest.Id,
                    ApprovedByAuthUserId = userChangeRequest.ChangedByAuthUserId,
                    ApprovedDateTime = DateTime.Now,
                    IsActive = true

                };
                UserChangeRequestApprovalRepository.Insert(userChangeRequestApproval);


            }

 public virtual void Insert(TEntity entity)
        {
            _dbSet.Add(entity);
        }

SaveContext方法

  public int SaveContext()
        {
            return _context.SaveChanges();
        }

整个方法的代码

 public IdentityResult ApproveUserChangeRequest(UserChangeRequest userChangeRequest, int approvedByAuthUserId, string authApplicationName)
        {
            var userChangeRequestRepository = UserChangeRequestRepository.GetAllAsList();
            var userChangeRequestApprovalRepository = UserChangeRequestApprovalRepository.GetAllAsList();
            var appSettingRepository = AppSettingRepository.GetAllAsList();
            var clientCompanyContactRepository = ClientCompanyContactRepository.GetAllAsList();
            var applicationUserRepo = ApplicationUserRepo.GetAllAsList();
           // int approvedByAuthUserID = GetApprovedByUserId(authApplicationName, approvedByAuthUserName);

            // Check if UserChangeRequest is still Pending
            bool isUserChangeRequestPending = userChangeRequestRepository.Any(x => x.Id == userChangeRequest.Id && x.ChangeStatus == "Pending");

            if (isUserChangeRequestPending && approvedByAuthUserId > 0)
            {
                // Inserting record in the UserChangeRequestApproval table
                   InsertUserChangeRequestApproval(userChangeRequest);
                   SaveContext();

                using (var userTransaction = Context.Database.BeginTransaction())
                {
                    using (var securityTransaction = _securityContext.Database.BeginTransaction())
                    {
                        try
                        {
                            //Get the Number of approval required for Internal and External Users
                            int? internalApprovalsRequired = GetApprovals("InternalUserChangeRequestApprovalsRequired", appSettingRepository);
                            int? externalApprovalsRequired = GetApprovals("ExternalUserChangeRequestApprovalsRequired", appSettingRepository);

                            //Get the name of the application the auth user belongs to
                            var authUserApplicationName = GetApplicationName(userChangeRequest.AuthUserId);

                            //Get the Number of approvals for the request

                            var numberOfAprovals = userChangeRequestApprovalRepository.Where(x => x.UserChangeRequestId == userChangeRequest.Id).Count();

                            //If the number of approvals is equal or greater than the Approvals required then Update AppUser or Contact details
                            if ((authUserApplicationName == "ArgentexTrader" && numberOfAprovals >= internalApprovalsRequired) || (authUserApplicationName == "ArgentexClient" && numberOfAprovals >= externalApprovalsRequired))
                            {
                                //Updating the clientcontact table
                                UpdateClientContact(userChangeRequest, clientCompanyContactRepository);


                                //Updating the auth user table
                                UpdateAuthUser(userChangeRequest);


                                //Updating the IdentityDB user table
                                UpdateIdentityDBUser(userChangeRequest, applicationUserRepo);

                                //Updating the UserChangeRequest table
                                userChangeRequest.ChangeStatus = "Approved";
                                UserChangeRequestRepository.Update(userChangeRequest);

                                SaveContext();
                                userTransaction.Commit();
                                securityTransaction.Commit();
                                return IdentityResult.Success;
                            }

                        }
                        catch (Exception ex)
                        {
                            userTransaction.Rollback();
                            securityTransaction.Rollback();
                            _logger.Error(ex);
                            return IdentityResult.Failed(new IdentityError { Description = ex.Message });
                        }
                    }
                }
            }
            return null;
        }

0 个答案:

没有答案