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

发布于 2025-02-10 06:49:00 字数 1025 浏览 6 评论 0原文

我在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);
            }
        }

The data is normal
data image

I tried
[HttpPost("ActualizarPedido")] // Internal server error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2025-02-17 06:49:00

在实体框架工作结构中,它跟踪具有跟踪器机制从数据库带来的实体。因此,即使其ID相同,您也不能直接更新客户端发送的对象。为此,请映射客户端发送到结果对象的pedido对象如下;

result.DireccionClientte = pedido.DireccionClientte;

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

另外,我应该指出,切勿将实体呈现给客户。您应该创建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 the result object as follows;

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.

吃兔兔 2025-02-17 06:49:00

事实证明,EF可以拥有多个实体,尝试捕获表明,这是工作代码:

[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");
                }
                
            }
        }

It turns out that EF can have more than one entity, a try catch showed me that, this is the working code:

[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");
                }
                
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文