StrataFrame Forum

Automatically creating a child BO

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

By fseider - 2/20/2008

I need to add a Child BO whenever I add a new Parent BO.  I have the proper parent/parent relationship setup, and can add the child BO manually within a form when testing.  (Parent = LocationBO, Child = PremisesBO)

The problem is the best place to do this, as in what Parent BO event or some other place?  I tried the AfterAddNew event, but the Parent PK is 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 FK  If I put the following code in the AfterSave, it works fine:

   Private Sub LocationsBO_AfterSave(ByVal e As MicroFour.StrataFrame.Data.AfterSaveUndoEventArgs) Handles Me.AfterSave

        Dim pBO As New PremisesBO
        pBO.Add()
        pBO.Name = "Premise"
        pBO.LocationsID = Me.LocationsID
        Try
            pBO.Save()
        Catch ex As Exception

        End Try
End Sub

The problem is that this event is also triggered when I delete my BO on a form:

    Private Sub btnLocationBO1Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocationBO1Delete.Click
        LocationBO1.DeleteCurrentRow()
        LocationBO1.Save()
    End Sub

So, what is the obvious approach I am overlooking?  I only need to create my ChildBO whenever I create my Parent.  The database handles the Child delete just fine with a Parent delete via a cascade.

Thanks in advance!

By Trent L. Taylor - 2/21/2008

I can't assign the proper PK to the Child BO FK  If I put the following code in the AfterSave, it works fine:

I think that you are making things more complicated than they need to be.  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.

StrataFrame will manage this foreign key value for you.  Within the designer of the child BO, you will setup the ParentRelationship property which defines how the child and parent relate to one another.  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.  At this point you can forget about having to worry with the parent PK and child PK.  When a new parent is created, as you noticed, it will use negative numbers.  So when you create a new child record, it will automatically update the childs foreign key with that negative number.  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.  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.  So you should not have to worry with this.

By fseider - 2/21/2008

Ah yes, the 'ParentBusinessObject' bit me.  Yep, I was making it more difficult for myself.  Thanks for getting me squared away!

Fred

By Trent L. Taylor - 2/21/2008

No problem Smile