且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF代码优先:INSERT语句与FOREIGN KEY约束冲突

更新时间:2023-02-25 22:07:14

我不知道您的 saveEntity方法是什么样的,但是我猜它叫

I don't know what your "saveEntity" method looks like but I'm guessing it calls

entityManager.saveChanges([order]);

如果是这样,那么问题是您只保存订单,而不是orderItem,因为您告诉它只保存一个订单。 Breeze会跟踪对entityManager的任何更改,因此通常更好的解决方案是让Breeze为您解决问题。即

If so, then the problem is that you are only saving the order and NOT the orderItem as well, because you told it to only save the one order. Breeze tracks any changes to the entityManager so a better solution is usually to just let Breeze figure it out for you. i.e.

entityManager.saveChanges();  or entityManager.saveChanges(null, ... );

这会将所有添加,修改或删除的记录保存在entityManager中。

Which will save all added, modified or deleted records in the entityManager.

或者,您可以指定要保存的所有实体。

Alternately you can specify all of the entities you want saved.

entityManager.saveChanges([order, orderItem1, orderItem2, ... ]);