asp.net - HttpPatch 未更新 .NET Core 中的数据
问题描述
我在 .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"
}
这些都不起作用。提前致谢。
解决方案
好像您在将对象保存到数据库之前忘记更新对象。
...
descriptionPatch.ApplyTo(video);
_context.Update(video); // add this before you save changes
await _context.SaveChangesAsync();
return Ok(video);
...
推荐阅读
- python - 对音频流使用多处理
- python - Wrong conversion of datetime timezone aware object in template Django
- qt - 图表中 LineSeries 的中继器
- r - 我们如何找到数据的平滑函数?
- r - 将数据设置为 xts 时处理“丢失”时间
- javascript - http响应中的角度怪异行为
- .net - 在多个嵌套哈希表、数组和当前循环中迭代循环的最佳实践是什么?
- haskell - 给定一个布尔值列表,返回一个布尔值
- java - 多个消费者线程整体消耗队列FIFO
- r - How to convert a list of matrices to a 3D matrix?