RegisterForeignKey


Author
Message
Aaron Young
Aaron Young
StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
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


Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.
Aaron Young
Aaron Young
StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
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.

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Glad it made sense Smile
Aaron Young
Aaron Young
StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
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


Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.
Aaron Young
Aaron Young
StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)StrataFrame User (435 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
Okay thanks - I appreciate that.

Aaron

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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();

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search