简体   繁体   English

在实体框架上进行更新时出现内部服务器错误

[英]Internal server error when doing Update on Entity Framework

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);我在 .net 核心中有一个 Angular 14 的前端和一个 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);
            }
        }

The data is normal数据正常数据图像

I tried [HttpPost("ActualizarPedido")] // Internal server error我试过 [HttpPost("ActualizarPedido")] // 内部服务器错误

In the Entity Framework working structure, it tracks an entity brought from the database with the tracker mechanism.在 Entity Framework 工作结构中,它使用跟踪器机制跟踪从数据库中带入的实体。 Therefore, you cannot directly update an object sent by the client, even if its id is the same.因此,您不能直接更新客户端发送的对象,即使其 id 相同。 To do this, map the pedido object sent by the client to the result object as follows;为此,将客户端发送的pedido对象映射到result对象,如下所示;

result.DireccionClientte = pedido.DireccionClientte;

//After that you can update your entity.
_context.Pedido.Update(result);
await _context.SaveChangesAsync();

Also, I should point out, never present the entity to the client.另外,我应该指出,永远不要将实体呈现给客户。 You should create DTO instead.您应该改为创建 DTO。

It turns out that EF can have more than one entity, a try catch showed me that, this is the working code:事实证明,EF 可以有多个实体,try catch 告诉我,这是工作代码:

[HttpPatch("ActualizarPedido")]         
        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 {
                try
                {
                    result.Detalle = pedido.Detalle;
                    result.TelefonoCliente = pedido.TelefonoCliente;
                    result.DireccionCliente = pedido.DireccionCliente;
                    result.NombreCliente = pedido.NombreCliente;
                    
                    _context.Pedido.Update(result);
                    await _context.SaveChangesAsync();
                    return Ok(result);
                }
                catch (System.Exception e)
                {
                    Console.WriteLine(e);
                    return StatusCode(500, "Internal Server Error");
                }
                
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM