Controlling Entity Posting Order to Avoid Constraint Violations
Due to database constraints, when you perform DML operations to save changes to a number of related entity objects in the same transaction, the order in which the operations are performed can be significant. If you try to insert a new row that contains foreign key references before you insert the row being referenced, the database can complain with a constraint violation.
By default, when you commit the transaction, the entity objects in the pending changes list are processed in chronological order, in other words, the order in which the entities were added to the list. This means that, for example, if you create a new Product and then a new Supplier related to the product, the new Product is inserted first, followed by the new Supplier.