Как обновить AppDBContext в ASP. NET Core Web API - PullRequest
0 голосов
/ 27 апреля 2020

Я совершенно новичок в ASP. NET, и я немного застрял в этом. Я создаю запись в моей БД при регистрации пользователя:

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

Я хочу изменить поле IsPasswordChanged в таблице UserActions на true, когда пользователь меняет свой пароль. Я пытаюсь что-то вроде:

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

        }

, но я не уверен, как поступить и обновить это до "true". Как мне обновить эту запись?

1 Ответ

1 голос
/ 27 апреля 2020

Вам нужно извлечь сущность useraction из таблицы, а затем установить для свойства IsPasswordChanged значение true.

Попробуйте это:

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

    }
...