【问题标题】:Concurrency failure while updating identity user in asp.net core在 asp.net 核心中更新身份用户时并发失败
【发布时间】:2021-07-26 15:24:32
【问题描述】:

在我的项目中,我使用的是 ASP.Net Core 身份。我有一个ApplicationUser 类,其中包含Education 列表。

public class ApplicationUser: IdentityUser, IEntity
    {
        public ApplicationUser()
        {
            Educations = new List<Education>();
        }
        public string Name { get; set; }   

        public DateTime DateofBirth { get; set; }
        
        public virtual List<Education> Educations { get; set; }
    }

教育

public class Education:IEntity
    {
        [Key]
        public Guid GUID { get; set; }
        public string Institution { get; set; }
        public string Degree { get; set; }
    }

我想通过 UserManager 更新用户教育列表。

public async Task<IActionResult> UpdateProfile(UpdateModel model)
        {
            ApplicationUser user = await _userManager.GetUserAsync(User);

            if (ModelState.IsValid)
            {
                user.Name = model.UserDetails.Name;
                user.DateofBirth = model.UserDetails.DateofBirth;
                user.Educations = model.UserDetails.Educations;
                
                var userPassword = _userManager.PasswordHasher.VerifyHashedPassword(user, 
                                    user.PasswordHash, model.Password);

                if (userPassword != PasswordVerificationResult.Success)
                {
                    ModelState.AddModelError(string.Empty, "Invalid Password.");
                    return Json("/Profile/Update");
                }
                else
                {
                    IdentityResult result = await _userManager.UpdateAsync(user);
                }
            }
            return Json("/Profile/Index");
        }

更新模型

public class UpdateModel
    {
        public ApplicationUser UserDetails { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }

更新时,如果我尝试添加新的 Education 对象,则会出现并发失败。但是已经存在的教育对象会正确更新。

【问题讨论】:

    标签: c# entity-framework-core asp.net-core-mvc asp.net-identity asp.net-core-3.1


    【解决方案1】:

    更新时,如果我尝试添加新的 Education 对象,则会出现并发失败。但是已经存在的教育对象会正确更新。

    您的意思是在同一个操作中创建和更新吗?

    可以尝试设置一个if else来判断数据库是否已经有用户,那么 创建或编辑模型,

    喜欢:

    public async Task<IActionResult> CreateOrUpdateProfile(UpdateModel model)
        {
            if (ModelState.IsValid)
            {              
                if (_context.ApplicationUsers.Any(x => x.Id == model.UserDetails.Id)) //judge
                {
                    //Create...
                }
                else 
                {
                    ApplicationUser user = await _userManager.GetUserAsync(User);           
                    //Edit...
                }             
            }
            return Json("/Profile/Index");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2019-03-11
      • 2023-03-06
      • 2020-10-30
      • 2021-05-28
      • 1970-01-01
      • 2019-03-24
      相关资源
      最近更新 更多