【问题标题】:HttpPatch not updating data in .NET CoreHttpPatch 未更新 .NET Core 中的数据
【发布时间】:2020-04-17 18:10:13
【问题描述】:

我在 .NET Core 中有以下代码

小米控制器

[HttpPatch]
[Route("description/{id}")]
public Task<ActionResult<Video>> UpdateVideoDescription(int id, [FromBody]JsonPatchDocument<Video> descriptionPatch)
    {
        return _repository.UpdateVideoDescription(id, descriptionPatch);
    }

小米仓库

public async Task < ActionResult < Video >> UpdateVideoDescription(int id, JsonPatchDocument < Video > descriptionPatch) {
 var video = await _context.Videos.FindAsync(id);
 descriptionPatch.ApplyTo(video);
 await _context.SaveChangesAsync();
 return Ok(video);
}

但由于某种原因,数据库中的描述没有更新...我尝试了以下请求

{
    "op": "replace",
    "path": "/description",
    "value": "New Description"
}

{
    "description" : "New Description"
}

这些都不起作用。 提前致谢。

【问题讨论】:

    标签: asp.net .net asp.net-core .net-core


    【解决方案1】:

    似乎您忘记在将对象保存到数据库之前对其进行更新。

    ...
    descriptionPatch.ApplyTo(video);
    _context.Update(video); // add this before you save changes
    await _context.SaveChangesAsync();
    return Ok(video);
    ...
    

    【讨论】:

    • 您的视频对象在您想要保存费用之前是否也发生了变化?在调试器中检查它
    • 没有。它没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2021-01-28
    • 2017-03-16
    • 1970-01-01
    • 2020-02-21
    相关资源
    最近更新 更多