进行内部服务器错误在实体框架上进行更新
我在Angular 14上有一个前端,并且在.NET Core中的EF上有一个后端,我不明白为什么代码到达_context.pedido.update(pedido)时爆炸了;
[HttpPatch("ActualizarPedido")] //500 (Internal Server Error)
public async Task<ActionResult<Pedidos>> ActualizarPedido([FromBody] Pedidos pedido)
{
if (string.IsNullOrEmpty(pedido.IdPedido.ToString()))
{
return BadRequest("Request is incorrect");
}
var result = await _context.Pedido.FindAsync(pedido.IdPedido);
if (result == null)
{
return NotFound();
}
else {
_context.Pedido.Update(pedido);
await _context.SaveChangesAsync();
return Ok(pedido);
}
}
我尝试了 [httppost(“实际izarpedido”)] //内部服务器错误
I have a front on angular 14 and a backend on EF in .net core, I don´t understand why the code blows when it reaches _context.Pedido.Update(pedido);
[HttpPatch("ActualizarPedido")] //500 (Internal Server Error)
public async Task<ActionResult<Pedidos>> ActualizarPedido([FromBody] Pedidos pedido)
{
if (string.IsNullOrEmpty(pedido.IdPedido.ToString()))
{
return BadRequest("Request is incorrect");
}
var result = await _context.Pedido.FindAsync(pedido.IdPedido);
if (result == null)
{
return NotFound();
}
else {
_context.Pedido.Update(pedido);
await _context.SaveChangesAsync();
return Ok(pedido);
}
}
I tried
[HttpPost("ActualizarPedido")] // Internal server error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在实体框架工作结构中,它跟踪具有跟踪器机制从数据库带来的实体。因此,即使其ID相同,您也不能直接更新客户端发送的对象。为此,请映射客户端发送到
结果
对象的pedido
对象如下;另外,我应该指出,切勿将实体呈现给客户。您应该创建DTO。
In the Entity Framework working structure, it tracks an entity brought from the database with the tracker mechanism. Therefore, you cannot directly update an object sent by the client, even if its id is the same. To do this, map the
pedido
object sent by the client to theresult
object as follows;Also, I should point out, never present the entity to the client. You should create DTO instead.
事实证明,EF可以拥有多个实体,尝试捕获表明,这是工作代码:
It turns out that EF can have more than one entity, a try catch showed me that, this is the working code: