Q:

A referential integrity constraint violation occurred mvc4

i have this classes :
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

and this is the PUT Method 
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

but when execute :db.Entry(order).State = EntityState.Modified
i have this error
A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.
thank u all

A:

It means that

either order.CustomerID != order.OrderCustomer.CustomerID
or OrderDetail.OrderID != OrderDetail.OrderDetailOrder.OrderID for at least one of the OrderDetails
or OrderDetail.ProductID != OrderDetail.OrderDetailProduct.ProductID for at least one of the OrderDetails

You must ensure that those values are equal before you attach the order to the context.

