This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I have a project which has Question entities and Message entities. There is a Many-to-one relationship such that a Question can have 0 or more Messages and each Message has exactly 1 Question. I then have the following situation:
Straight after running this code I look at question.Messages and it is empty (but the message is in the database with the correct QuestionId). If instead I use this code:
then question.Messages shows up as having one item which is what I want. So my question is, is this expected behaviour? Should these two pieces of code give this subtle difference in behaviour? I've obviously fixed the issue I was having by using the second code snippet, but I think there may be a number of other instances in my code were I may have similar issues. Are there any 'best practice' tips for adding entities to a UOW? Thanks Andrew |
|
|
Hi Andrew, Yes, this is the expected behaviour. If you are wiring up by Id prior to adding the entity to the UnitOfWork then no subsequent wire-up will occur against the parent's child collection. If you wire up by object then it will automatically attach the new object to the UnitOfWork of the parent and perform the child collection wire-up. Just to add yet another scenario to the mix, if you assign by Id when the object is already attached to a UnitOfWork then we will wire-up the child collection if the parent is already loaded into the current UnitOfWork and its child collection is also already loaded. If its in the UnitOfWork but the child collection is still lazy then we will defer loading the child collection but note the add/remove so it can be applied when the child collection eventually gets loaded. In terms of best practice, it depends what you want to do. If you expect to have the child object accessible in the parent child collection within the same UOW scope then assign by object reference rather than Id. If you want to avoid the overhead associated with the association wire-up then assign by Id as you have done.
|
|
|
Thanks for the info Jeremy. Andrew |
|