ddp74 wrote:
Well - yes - I know how to force it that way. I meant is there a way in code to tell it respect the ordering without me now having to go through every instance where we do saves like this and add a hundred uow.Commit lines just in case this happens again?
Or is there a way I can inspect/override the graph traversal and see why it's picking the wrong order in this instance?
And, to be clear, even though the SubRecipe entities are technically dependent on the UOMs in this instance, there's no guaranteeing whether or not it decides to save them in a different order than stated in code and that's normal behaviour?
If you don't use batching, the system will traverse the entities recursively and based on each entity and the reachable entities in the graph it's part of, will produce a queue for persistance in the right order. If you do use batching, the entities will grouped together in batches which are scheduled in the order that is derived from the relationship direction (in your case it's going to persist subrecipes after uoms).
If you add the entities in a certain order and not specify any recurse action, and your code uses the default, which is false so there's no graph traversal/recurse happening, they're persisted in that order, so recipeUOMs first, then SubRecipes.
It's indeed weird you run into this.
If you after this code:
UnitOfWork2 work = new UnitOfWork2();
work.AddCollectionForSave(recipeUoms);
work.AddCollectionForSave(subRecipes);
do:
work.ConstructSaveProcessQueues();
var insertQueue = work.GetInsertQueue();
You should see the elements in the order they'll be inserted.
As adding them individually does work (which does use recurse) my suspicion is that the subrecipes do have a reference to a recipeUOM.
You can check the queues for both actions using the 2 lines above.
If you can reproduce this with a few entities every time, we'll see if we can reproduce it with the same setup. (tho we need to know which code creates the entities so we know they don't have references to each other which are utilized in a traversal over the graphs).
It is odd tho that your code worked fine for some time and now all of a sudden fails.