_Context.SaveChangesAsync()未在命令行中执行

时间:2018-04-22 10:43:58

标签: entity-framework-core asp.net-core-2.0

我在项目的很多地方都使用了_Context.SaveChangesAsync()并且工作正常,除了这里

//hash the password change the bool to false set the salt save
                        await TryUpdateModelAsync<User>(user);
                        Hash hash = new Hash();
                        string salt = hash.CreateSalt();
                        user.Salt = salt;
                        user.Password = hash.HashPassword(NewPassword, salt);
                        user.ChangePassword = false;
                        await _Context.SaveChangesAsync();

模型状态为true,它应该工作正常,但即使在命令行(Microsoft.EntityFrameworkCore.Database.Command)它根本没有显示,数据库没有更新,没有错误或异常正在抛出

任何想法?

这是整个班级

public class ChangePasswordModel : PageModel
{
    public string CurrentPassword { set; get; }
    public string NewPassword { set; get; }
    public User user { set; get; }

    private readonly PROJECTDATABASE_MDFContext _Context;
    public ChangePasswordModel(PROJECTDATABASE_MDFContext databasecontext)
    {
        _Context = databasecontext;
    }

    public void OnGet()
    {

    }

    public async Task<IActionResult> OnPostAsync()
    {
        int TempId = 0;
        try
        {
            TempId = Convert.ToInt32(User.FindFirst(claim => claim.Type == System.Security.Claims.ClaimTypes.NameIdentifier)?.Value);
        }
        catch { }
        if (TempId > 0)
        {
            user = new User();
            user = await _Context.User.AsNoTracking().Include(m => m.Role).FirstOrDefaultAsync(m => m.UserId == TempId);

            if (user != null)
            {
                CurrentPassword = Request.Form["CurrentPassword"];
                NewPassword = Request.Form["NewPassword"];

                //if the password is hashed
                if (user.ChangePassword == false)
                {
                    Hash hash = new Hash();
                    CurrentPassword = hash.HashPassword(CurrentPassword, user.Salt);
                }

                if (user.Password == CurrentPassword)
                {
                    if(NewPassword.Length >= 8)
                    {
                        //hash the password change the bool to false set the salt save
                        await TryUpdateModelAsync<User>(user);
                        Hash hash = new Hash();
                        string salt = hash.CreateSalt();
                        user.Salt = salt;
                        user.Password = hash.HashPassword(NewPassword, salt);
                        user.ChangePassword = false;
                        await _Context.SaveChangesAsync();

                        if (user.Role.RoleName == "Student")
                        {
                            return RedirectToPage("Users/StudentProfile", new { id = user.UserId });

                        }
                        else
                        {
                            return RedirectToPage("Users/StaffMemberProfile", new { id = user.UserId });

                        }

                    }
                    else
                    {
                        ModelState.AddModelError("NewPassword", "Password Must be at least 8 characters!");
                        return Page();
                    }

                }
                else
                {
                    ModelState.AddModelError("CurrentPassword", "Password is not Correct!");
                    return Page();
                }
            }
        }
        return Page();
    }
}

在保存之前,用更新的值正确填充了用户,因此问题不在哈希方法

1 个答案:

答案 0 :(得分:0)

问题在于您加载User实体的行:

user = await _Context.User.AsNoTracking().Include(m => m.Role).FirstOrDefaultAsync(m => m.UserId == TempId);

您使用AsNoTracking()扩展调用,告知实体框架不跟踪返回实体的任何更改。因此,要解决此问题,只需删除不必要的AsNoTracking()调用:

user = await _Context.User.Include(m => m.Role).FirstOrDefaultAsync(m => m.UserId == TempId);