0

我有这个课程:

Public Class Customer
      Public Property CustomerID As Integer
      Public Property CustomerName As String
      Public Property OrdersList As ICollection(Of Order)
End Class

Public Class Order
          Public Property OrderID As Integer
          Public Property CustomerID As Integer
          Public Property OrderTotal As Decimal
          Public Property OrderDetails As ICollection(Of OrderDetail)
          Public Property OrderCustomer As Customer
End Class

Public Class OrderDetail

    Public Property OrderDetailID As Integer
    Public Property OrderID As Integer
    Public Property ProductID As Integer
    Public Property Price As Decimal
    Public Property Quantity As Decimal
    Public Property LineTotal As Decimal

    Public Property OrderDetailOrder As Order
    Public Property OrderDetailProduct As Product

End Class

Public Class Product
    Public Property ProductID As Integer
    Public Property ProductName As String
    Public Property ProductPrice As Decimal
    Public Property OrderDetails As ICollection(Of OrderDetail)
End Class

这就是 PUT 方法

Function PutOrder(ByVal id As Integer, ByVal order As Order) As HttpResponseMessage
        If ModelState.IsValid And id = order.OrderID Then
            db.Entry(order).State = EntityState.Modified
            Try
                db.SaveChanges()
            Catch ex As DbUpdateConcurrencyException
                 Return Request.CreateResponse(HttpStatusCode.NotFound)
            End Try
            Return Request.CreateResponse(HttpStatusCode.OK)
        Else
            Return Request.CreateResponse(HttpStatusCode.BadRequest)
        End If
End Function

但是当执行时:db.Entry(order).State = EntityState.Modified 我有这个错误

发生参照完整性约束冲突:定义参照约束的属性值在关系中的主体对象和从属对象之间不一致。

谢谢大家

4

2 回答 2

0

我认为这是解决方案

    Dim x = db.Orders.Include(Function(a) a.OrderDetails).Include(Function(o) o.OrderCustomer).FirstOrDefault
    db.Entry(x).CurrentValues.SetValues(order)

谢谢你@Slauma,你的建议很有帮助

于 2013-02-18T17:14:28.347 回答
0

代表着

  • 任何一个order.CustomerID != order.OrderCustomer.CustomerID
  • OrderDetail.OrderID != OrderDetail.OrderDetailOrder.OrderID至少一个 OrderDetails
  • OrderDetail.ProductID != OrderDetail.OrderDetailProduct.ProductID至少一个 OrderDetails

order在将 附加到上下文之前,您必须确保这些值相等。

于 2013-02-17T16:55:01.313 回答