Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 如何在ASP.NET核心Web API中更新AppDBContext_Asp.net Core_Asp.net Web Api_Entity Framework Core - Fatal编程技术网

Asp.net core 如何在ASP.NET核心Web API中更新AppDBContext

Asp.net core 如何在ASP.NET核心Web API中更新AppDBContext,asp.net-core,asp.net-web-api,entity-framework-core,Asp.net Core,Asp.net Web Api,Entity Framework Core,我是ASP.NET的新手,对此我有点困惑。 注册用户时,我正在数据库中创建一个条目: private async Task<bool> CreateEntryInUserActions(AppUser user) { var entity = new UserActionEntity { UserId = user.Id, };

我是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;
         }
专用异步任务CreateEntryInUserActions(AppUser)
{
var entity=new UserActionEntity
{
UserId=user.Id,
};
wait_context.tbl_UserActions.AddAsync(实体);
wait_context.SaveChangesAsync();
返回true;
}
我想在用户更改密码时将UserActions表中的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
                .Where(x => x.UserId.ToString() == userId).Select(x => x.IsPasswordChanged);

        }
private异步任务UpdateUserAction()
{
var userId=\u httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;//提供当前用户的id
var user=\u context.tbl\u UserActions
.Where(x=>x.UserId.ToString()==UserId)。选择(x=>x.IsPasswordChanged);
}

但我不知道如何继续并将其更新为“true”。如何更新此条目?

您需要从表中获取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();
         }

    }
private异步任务UpdateUserAction()
{
var userId=\u httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;//提供当前用户的id
var user=\u context.tbl\u UserActions.FirstOrDefault(x=>x.UserId.ToString()==UserId);
if(user!=null)//检查记录是否不为null
{
user.IsPasswordChanged=true;//将列设置为所需的值
_context.tbl_UserActions.Update(用户);
wait_context.SaveChangesAsync();
}
}

哦,我明白了。我认为我必须查询用户,然后使用一些预定义的方法来更新字段。但这很有效。谢谢!:)