首页 > 解决方案 > Count 不考虑新插入的记录

问题描述

插入第一条记录后,即使插入后计数也显示为 0。执行 SaveContext() 后,我可以看到插入的记录。所以看起来 userChangeRequestApprovalRepository 没有用新插入的数据刷新。像下面的语句那样做 count + 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;
        }

标签: c#linq

解决方案


推荐阅读