StrataFrame Forum

RegisterForeignKey

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

By Aaron Young - 7/3/2008

Hi,

I have tried to use RegisterForeignKey but when I try a FillByParentPrimaryKey on the child it throws an error saying the ParentRelationship must be set first. I set the foreign key with the following code:-

parentBO.RegisterForeignKey(childBO, "FKField");

Then when I run the following code it throws the error.

childBO.FillByParentPrimaryKey(parentBO.PKField);

I guess I am doing something wrong but I can't see what. The RegisterForeignKey is definitely run first.

Any help would be appreciated.

Aaron

By Trent L. Taylor - 7/3/2008

This is not a relationship definition, but a foreign key managment definition.  The FillBy methods will still only work if a parent relationship is setup.  The RegisterForeignKey methods will automatically propgate and manage foreign key values.  For example, if you add a parent record and then a child with the foreign key registered, the parent will have a PK of -1, let's say.  When a child record is created, it will automatically update the foreign key field with a -1.  When the parent (or even the child BO) is saved and the real parent PK is assigned and retrieved, the BOs will automatically update the child BOs before they are committed to the database...thus foreign key management...not parent relationship managment.  This is more akin to a foreign key contraint than a parent relationship definition.
By Aaron Young - 7/3/2008

Thanks for the info. I had guessed this but it is no big deal as the foreign key management is more important than the relationship management - it is simple to substitute the FillBy method with a custom one of my own.

Thanks.

By Trent L. Taylor - 7/4/2008

Glad it made sense Smile
By Aaron Young - 7/4/2008

I think I am still doing something wrong. When I add a record to the child the FK is not updated.

Using the SF sample database, I created a form with two BOs (Customers and CustomerNotes). CustomersBO has a FillAll() which simply reads all records. When I run the following code it throws an error during the save.

            customersBO1.RegisterForeignKey(customerNotesBO1, "cn_cust_pk");
            customersBO1.FillAll();
            customersBO1.MoveFirst();
            MessageBox.Show(customersBO1.cust_pk.ToString());
            customerNotesBO1.Add();
            customerNotesBO1.cn_Version = 99;
            customerNotesBO1.Save();

The customersBO1 definitely has a current record yet the save throws the following error:-

"The INSERT statement conflicted with the FOREIGN KEY constraint "Fk_Customers_Notes". The conflict occurred in database "StrataFrameSample", table "dbo.Customers", column 'cust_pk'.
The statement has been terminated."

Am I doing something wrong?

Thanks in advance,

Aaron

By Trent L. Taylor - 7/6/2008

I will try and setup a sample and post it in the next couple of days so this will be a little easier to understand instead of me just posting snippets.  That way you (and other developers as well) have an example to go by.
By Aaron Young - 7/6/2008

Okay thanks - I appreciate that.

Aaron

By Trent L. Taylor - 7/7/2008

OK...there is one piece missing here as the RegisterForeignKey behaves slightly different than the ParentRelationship.  When a child has the ParentBusinessObject instance set, it automatically pulls the PK of the parent.  In this case, you will need to manually set the initial FK value on the child.  But once set (even for a new record with a negative value) it will keep them in sync and propgate the new value through the child and enforce the save order in the instance the child is saved prior to the parent.  So you code would look like this:

customersBO1.RegisterForeignKey(customerNotesBO1, "cn_cust_pk");
customersBO1.FillAll();
customersBO1.MoveFirst();
MessageBox.Show(customersBO1.cust_pk.ToString());
customerNotesBO1.Add();
customerNotesBO1.cn_cust_pk = customersBO1.cust_pk;
customerNotesBO1.cn_Version = 99;
customerNotesBO1.Save();