Child Object Needs It's Own ID On New?

Child Object Needs It's Own ID On New?

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


Wbmstrmjb posted on Wednesday, October 15, 2008

Why does the editable child template (Codesmith Ricky 2.0) generate the "New" procedure requesting it's own ID?  Shouldn't "New" not know it's ID until the object is saved?  It seems that it would take it's parent's ID possibly (though the parent may not yet be saved either).  Is this a bug in the template or am I missing something?

Thanks,

Mike

JoeFallon1 replied on Wednesday, October 15, 2008

I use SQL Server Identity values for my keys. So that means that an in memory object does not have a real ID. But I still assign a valid ID to each object - typically I use (-1*coll.Count) as the ID which guarantees me a temporary unique ID. When the whole object is saved, you are right, the parent is aved first, gets the real ID from the DB and then passes it down to each child as a foreign key. Then each child inserts itself and gets a real ID.

Note that the parent ID is not the same as the child unique ID! It is the foreign key.

Joe

 

rsbaker0 replied on Wednesday, October 15, 2008

^^^^

I have use a similar technique but only use each "pseudo" ID once per application session to avoid the unlikely possibility I might have two new objects with the same ID.

So, I start with -1 for each object Type and decrement each time a new temporary key is requested.

Copyright (c) Marimer LLC