_Context.SaveChangesAsync() is not executing in command line

asp.net-core-2.0 entity-framework-core

Question

I have used _Context.SaveChangesAsync() in a lot of places in my project and it work fine, except here

//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();

The Model state is true, and it should be working fine but even in the command line (Microsoft.EntityFrameworkCore.Database.Command) it doesn't show at all and the database is not updating, no error or exception is being thrown

any idea ?

this is the whole class

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();
    }
}

before the save the user is populated correctly with the updated values so the problem is not in the hash method

1
1
4/22/2018 11:32:40 AM

Accepted Answer

The problem is in the line where you load User entity:

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

You use AsNoTracking() extension call which tells Entity Framework not to track any changes for returned entity. So to fix the problem just remove unnecessary AsNoTracking() call:

user = await _Context.User.Include(m => m.Role).FirstOrDefaultAsync(m => m.UserId == TempId);
0
4/22/2018 1:56:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow