Count不考虑新插入的记录

插入第一条记录后,即使插入后,计数仍显示为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;
        }
jinruien0219 回答:Count不考虑新插入的记录

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3153274.html

大家都在问