Identity Column

Identity Column

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


jfreeman posted on Wednesday, May 18, 2011

I have a parent child scenario where the user enters info for the parent but the ID (which is an Identity column in SQL) does not get assigned until the record is saved.  How do I handle this on the child?  Do I setup a dummy ID until the save?  If so, how do I pass the ID down to the children after the save?  I've looked around in the forum but could not find an answer.  I figure I must be missing something.  Any help would be appreciated.  Code samples would be even more appreciated.  Thanks.

Jonathan

RockfordLhotka replied on Wednesday, May 18, 2011

Normally the parent will insert itself into the database so it has its new Id value.

Then when the parent calls FieldManager.UpdateChildren (or explicitly saves each child), it passes a reference to itself as a parameter. This way the child objects have a reference to the parent while they are being saved, and the parent object has a valid Id property.

This is documented in the Using CSLA 4: Data Access ebook.

Also, the ProjectTracker code in svn (not yet in the Samples download) uses this technique when saving ProjectEdit or ResourceEdit objects.
http://www.lhotka.net/cslacvs/viewvc.cgi/core/trunk/Samples/NET/cs/ProjectTracker/

Copyright (c) Marimer LLC