Error: An error occurred while saving the entity changes in .NET

Error: An error occurred while saving the entity changes in .NET

我在 post 向我的 API 发送数据时遇到此错误:

An error occurred while saving the entity changes. See the inner exception for details.
 
---> Microsoft.Data.SqlClient.SqlException (0x80131904): 

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_OrderLine_Order_OrderID". 

The conflict occurred in database "AnimimoMicroservicesNewOrderServiceContext-feca99ec-8feb-4680-b1f5-f1a32825a8c1", table "dbo.Order", column 'OrderID'.

这是 HttpPost 方法,我在其中 post 将我的模型连接到数据库:

// POST: api/Orders
        [HttpPost]
        public async Task<ActionResult<Order>> PostOrder(PostOrderDTO postOrderDto)
        {

            var order = new Order(postOrderDto.Identifier, postOrderDto.Customer);

            var json = JsonConvert.SerializeObject(order);

            var content = new StringContent(json, Encoding.UTF8, "application/json");

            _context.Order.Add(order);

            var basket = await GetBasketItems(postOrderDto.Identifier);
            var newOrderId = order.OrderID;

            foreach (var item in basket.Items)
            {
                var orderLine = new OrderLine
                {
                    OrderID = newOrderId,
                    ProductId = item.ProductId,
                    Quantity = item.Quantity
                };

                _context.OrderLine.Add(orderLine);
            };

            await _context.SaveChangesAsync();

            return CreatedAtAction("GetOrder", new { id = order.OrderID }, order);
        }

'OrderID' 是在 post 下订单时自动生成的,但我需要生成相同的 OrderID post 并保存在 OrderLine 中,这是我的订单数据库看起来像: OrderDB 因此,当我 post 并保存到 Order 时生成的 OrderID 是我试图进入我的 OrderLine 数据库的内容,它在我的数据库中看起来像这样: OrderLineDB

它只说 NULL,然后我在 POST 方法中尝试了下面的代码 post OrderID 但是得到了如上所述的错误,我正在尝试解决这个问题

这是我的 Order.cs 型号:

public class Order
    {
        public Order(int orderID, string identifier, string customer)
        {
            OrderID = orderID;
            Identifier = identifier;
            Customer = customer;
        }

        public Order(string identifier, string customer)
        {
            Identifier = identifier;
            Customer = customer;
        }

        [Key]
        public int OrderID { get; set; }
        public string Identifier { get; set; }

        public string Customer { get; set; }

        //[NotMapped]
        public IEnumerable<OrderLine> Items { get; set; } = new List<OrderLine>();
    }

Order.cs 模型中的 'OrderID' 是 OrderLine table 的外键,如下所示:

public class OrderLine
    {
        public int Id { get; set; }
        public int OrderID { get; set; }
        public int ProductId { get; set; }
        public int Quantity { get; set; }
    }

我该如何解决这个问题?我希望使用相同的 Post 方法将我的订单 table 生成的 OrderID post 编辑到我的 OrderLine table 但我只收到上述错误,我可以吗解决这个问题?提前感谢您的帮助

添加订单时您没有保存更改。这意味着订单对象还没有 id。如果您在 _context.Order.Add(order); 之后和将 id 添加到订单行之前保存,这将得到解决。

您必须修复导航属性

public class OrderLine
    {
        public int Id { get; set; }
       public int Quantity { get; set; }

        public int OrderID { get; set; }
        public int ProductId { get; set; }

         public  virtual Order Order { get; set; }
        public virtual Product { get; set; }
       
    }

并在代码中删除 _context.Order.Add(order);

            var basket = await GetBasketItems(postOrderDto.Identifier);
  
            foreach (var item in basket.Items)
            {
                var orderLine = new OrderLine
                {
                    Order = order,
                    ProductId = item.ProductId,
                    Quantity = item.Quantity
                };

                _context.OrderLine.Add(orderLine);
            };

            await _context.SaveChangesAsync();

出于好奇,这是干什么用的?

var json = JsonConvert.SerializeObject(order);
 var content = new StringContent(json, Encoding.UTF8, "application/json");