0

I am quite new to ASP.NET and I am bit stuck with this. I am creating an entry in my DB while registering the user:

private async Task<bool> CreateEntryInUserActions(AppUser user)
        {
                var entity = new UserActionEntity
                {
                    UserId = user.Id,
                };

                await _context.tbl_UserActions.AddAsync(entity);
                await _context.SaveChangesAsync();

                return true;
         }

I want to change the IsPasswordChanged field in the UserActions table to true when a user changes his/her password. I am trying something like:

private async Task<bool> UpdateUserAction()
        {
            var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; // gives me current user's id

            var user = _context.tbl_UserActions
                .Where(x => x.UserId.ToString() == userId).Select(x => x.IsPasswordChanged);

        }

but I am not sure how to proceed and update this to "true". How do I update this entry?

1 Answer 1

1

You need to fetch the useraction entity from the table and then set the IsPasswordChanged property to true.

Try this:

private async Task<bool> UpdateUserAction()
    {
        var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; // gives me current user's id

        var user = _context.tbl_UserActions.FirstOrDefault(x => x.UserId.ToString() == userId);
        if(user != null) //check if the record is not null
        {
            user.IsPasswordChanged = true; // set the column to desired value
            _context.tbl_UserActions.Update(user);
            await _context.SaveChangesAsync();
         }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see. I though I had to query over the user and then use some pre-defined method to update the field. But this worked nicely. Thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.