﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » Business Objects and Data Access (How do I?)  » Automatically creating a child BO</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sat, 16 May 2026 05:24:37 GMT</lastBuildDate><ttl>20</ttl><item><title>Automatically creating a child BO</title><link>http://forum.strataframe.net/FindPost14472.aspx</link><description>I need to add a Child BO whenever I add a new Parent BO.&amp;nbsp; I have the proper parent/parent relationship setup, and can add the child BO manually within a form when testing.&amp;nbsp; (Parent = LocationBO, Child = PremisesBO)&lt;/P&gt;&lt;P&gt;The problem is the best place to do this, as in what Parent BO event or some&amp;nbsp;other place?&amp;nbsp; I tried the AfterAddNew event, but the Parent PK is&amp;nbsp;equal to a -1 which I suspect is because the Parent has yet to be saved, so I can't assign the proper PK to the Child BO&amp;nbsp;FK&amp;nbsp; If I put the following code in the AfterSave, it works fine:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Private Sub LocationsBO_AfterSave(ByVal e As MicroFour.StrataFrame.Data.AfterSaveUndoEventArgs) Handles Me.AfterSave&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim pBO As New PremisesBO&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pBO.Add()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pBO.Name = "Premise"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pBO.LocationsID = Me.LocationsID&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Try&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pBO.Save()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Catch ex As Exception&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Try&lt;BR&gt;End Sub&lt;/P&gt;&lt;P&gt;The problem is that this event is also triggered when I delete my BO on a form:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub btnLocationBO1Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocationBO1Delete.Click&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LocationBO1.DeleteCurrentRow()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LocationBO1.Save()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;BR&gt;&lt;/P&gt;&lt;P&gt;So, what is the obvious approach I am overlooking?&amp;nbsp; I only need to create my ChildBO whenever I create my Parent.&amp;nbsp; The database handles the Child delete just fine with a Parent delete via a cascade.&lt;/P&gt;&lt;P&gt;Thanks in advance!</description><pubDate>Thu, 21 Feb 2008 13:13:02 GMT</pubDate><dc:creator>fseider</dc:creator></item><item><title>RE: Automatically creating a child BO</title><link>http://forum.strataframe.net/FindPost14499.aspx</link><description>No problem :)</description><pubDate>Thu, 21 Feb 2008 13:13:02 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: Automatically creating a child BO</title><link>http://forum.strataframe.net/FindPost14497.aspx</link><description>Ah yes, the 'ParentBusinessObject' bit me.&amp;nbsp; Yep,&amp;nbsp;I was making it more difficult for myself.&amp;nbsp; Thanks for getting me squared away!&lt;/P&gt;&lt;P&gt;Fred</description><pubDate>Thu, 21 Feb 2008 11:34:04 GMT</pubDate><dc:creator>fseider</dc:creator></item><item><title>RE: Automatically creating a child BO</title><link>http://forum.strataframe.net/FindPost14481.aspx</link><description>[quote]I can't assign the proper PK to the Child BO&amp;nbsp;FK&amp;nbsp; If I put the following code in the AfterSave, it works fine:[/quote]&lt;/P&gt;&lt;P&gt;I think that you are making things more complicated than they need to be.&amp;nbsp; First, the documentation and samples show you about the negative numbers and how this works...and you will want to search out here on the forum as well as there are many other posts regarding parent and children relationships and how StrataFrame will manage this value for you.&lt;/P&gt;&lt;P&gt;StrataFrame will manage this foreign key value for you.&amp;nbsp; Within the designer of the child BO, you will setup the ParentRelationship property which defines how the child and parent relate to one another.&amp;nbsp; When you drop an instance (or create an instance in code) you will then set the ParentBusinessObject property of the child to the instance of the parent.&amp;nbsp; At this point you can forget about having to worry with the parent PK and child PK.&amp;nbsp; When a new parent is created, as you noticed, it will use negative numbers.&amp;nbsp; So when you create a new child record, it will automatically update the childs foreign key with that negative number.&amp;nbsp; But here is where SF really shines, even if you manually try to save the child BO programmatically first, it recognizes that the parent is dirty and ensures that its parent saves before itself.&amp;nbsp; Once the parent saves (and this can nest however deep you need to) it will automatically propogate the new PK of parent to the foreign keys of children....thus replacing the negative value with the actual PK.&amp;nbsp; So you should not have to worry with this.</description><pubDate>Thu, 21 Feb 2008 09:13:53 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>