Updating Child objects on Save()

Updating Child objects on Save()

Old forum URL: forums.lhotka.net/forums/t/8129.aspx


jsavold posted on Friday, December 11, 2009

OrderCollection orders = OrderCollection.New();
Order order = Order.New();

orders.Add(order);
orders = orders.Save();

The order object get a temp id until it is saved through the parent. After Save() is called the child object in the collection has the new id from the DB but the original object still has the temp id. How do get the order object and the child order to point to the same object?

jrose replied on Friday, December 11, 2009

Please help. I am having the same problem and I am really stuck. My parent's id matches the children's id . . .until sometime during the save process. . . it appears to be 'flipping' the parent id there anyway. Any suggestions would be greatly appreciated.

RockfordLhotka replied on Friday, December 11, 2009

I suggest you read chapters 1-5 (in particular 4-5) of the Expert 2008 Business Objects book, as it walks through parent-child class relationships pretty clearly. You might also look at chapter 18, which discusses data access.

The Core CSLA .NET 3.8 video series (videos 2 and 3) also cover parent-child relationships in some detail.

The root object is the only thing directly saved by the data portal. The root object needs to call the data portal (or in a BusinessBase the FieldManager, or in the case of a BusinessListBase, the base class) so the data portal invokes Child_XZY methods on each child object.

Only the root object will have DataPortal_XYZ methods, the child objects will have Child_XYZ methods.

Normally the root object passes a reference to itself to the data portal when saving the child objects, so the child objects have access to the root object's properties (like its Id property).

This only works, of course, if you've already set the root object's Id property before triggering the save of the child objects.

rxelizondo replied on Saturday, December 12, 2009

jsavold:

The order object get a temp id until it is saved through the parent. After Save() is called the child object in the collection has the new id from the DB but the original object still has the temp id. How do get the order object and the child order to point to the same object?


Assuming that I understood your question correctly.....

Just like the "orders" object its a *new* object after the call:

orders = orders.Save();

The child objects in the "orders" collection are also all new. Once you call the Save() method and get the new "orders" object all reference to the previously existing "orders" object and any of its children have to be updated.

Copyright (c) Marimer LLC