StrataFrame Forum

Grandchild BO does not get foreign key value from Parent Insert

http://forum.strataframe.net/Topic21239.aspx

By Leonard P. - 12/16/2008

Hello,



I have a situation where I need to insert new rows of 3 related BOs on .Save. BOs parent/child relationship is setup correctly in the BO Mapper, and it follows the following path:



BO_1

P1_Key (PK)



BO_2 (Child to BO_1)

P2_Key (PK)

P1_Key (FK to BO_1)



BO_3 (Child to BO_2)

P3_Key (PK)

P1_Key (FK to BO_2)

P2_Key (FK to BO_2)



When I call Save to add new rows of all 3 BOs, the first 2 inserts complete successfully, but BO_3 insert fails, because it doesn't get newly inserted P1_Key. I realize that the design could be improved by simply getting rid of P1_Key FK in BO_3, but lets say for now that I need to stick to this design to accommodate some obscure requirements.

So, why doesn't BO_3 get P1_Key?



Thanks


By Charles R Hankey - 12/16/2008

I'm sure you'll get replies with a lot more SF knowledge than mine, but my first reaction would be I wouldn't expect bo3 to get the pk from bo1 automatically, since the parentchild relationship is only between bo2 and bo3. If you are getting bo2's pk as an fk in bo3 I'd say everything is working as designed. You might want to grab the bo1fk from bo2 on the save of bo3, but I dont think the framework is going to give you two fks.


By Alex Luyando - 12/17/2008

Hi all -



Does anyone know if the use of RegisterForeignKey() would be a solution to this problem?
By Trent L. Taylor - 12/17/2008

Yes, the RegisterForeignKey method would be a great option here.  The RegisterForeignKey method allows you to create as many foreign relationships as you need to any one (or multiple) BOs.  This is the purpose for this method.  The only difference from the single ParentBusinessobject funtionality is that when you adda  new record, you must set the foreign key field's value (even if it is a new negative PK value) and then the RegisterForeignKey logic will handle everything else from there.  Just to illustrate my point:

MyParentBO.ForeignKeyField = MyInitialValue