﻿<?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?)  » RegisterForeignKey</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Mon, 08 Jun 2026 22:44:54 GMT</lastBuildDate><ttl>20</ttl><item><title>RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17624.aspx</link><description>Hi,&lt;/P&gt;&lt;FONT size=2&gt;&lt;P&gt;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:-&lt;/P&gt;&lt;P&gt;parentBO.RegisterForeignKey(childBO, &lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;&lt;FONT color=#a31515 size=2&gt;"FKField"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Then when I run the following&amp;nbsp;code it throws the error.&lt;/P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;P&gt;childBO.FillByParentPrimaryKey(parentBO.PKField);&lt;/P&gt;&lt;P&gt;I guess I am doing something wrong but I can't see what. The RegisterForeignKey is definitely run first.&lt;/P&gt;&lt;P&gt;Any help would be appreciated.&lt;/P&gt;&lt;P&gt;Aaron&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;</description><pubDate>Mon, 07 Jul 2008 15:34:25 GMT</pubDate><dc:creator>Aaron Young</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17690.aspx</link><description>OK...there is one piece missing here as the RegisterForeignKey behaves slightly different than the ParentRelationship.&amp;nbsp; When a child has the ParentBusinessObject instance set, it automatically pulls the PK of the parent.&amp;nbsp; In this case, you will need to manually set the initial FK value on the child.&amp;nbsp; 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.&amp;nbsp; So you code would look like this:&lt;/P&gt;&lt;P&gt;[codesnippet]customersBO1.RegisterForeignKey(customerNotesBO1, "cn_cust_pk");&lt;BR&gt;customersBO1.FillAll();&lt;BR&gt;customersBO1.MoveFirst();&lt;BR&gt;MessageBox.Show(customersBO1.cust_pk.ToString());&lt;BR&gt;customerNotesBO1.Add();&lt;BR&gt;&lt;FONT style="BACKGROUND-COLOR: #ffff11"&gt;customerNotesBO1.cn_cust_pk = customersBO1.cust_pk;&lt;/FONT&gt;&lt;BR&gt;customerNotesBO1.cn_Version = 99;&lt;BR&gt;customerNotesBO1.Save();[/codesnippet]</description><pubDate>Mon, 07 Jul 2008 15:34:25 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17659.aspx</link><description>Okay thanks - I appreciate that.&lt;/P&gt;&lt;P&gt;Aaron</description><pubDate>Mon, 07 Jul 2008 05:31:17 GMT</pubDate><dc:creator>Aaron Young</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17652.aspx</link><description>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.&amp;nbsp; That way you (and other developers as well) have an example to go by.</description><pubDate>Sun, 06 Jul 2008 19:01:35 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17636.aspx</link><description>I think I am still doing something wrong. When I add a record to the child the FK is not updated.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;FONT size=2&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; customersBO1.RegisterForeignKey(customerNotesBO1, "cn_cust_pk");&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; customersBO1.FillAll();&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; customersBO1.MoveFirst();&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; MessageBox.Show(customersBO1.cust_pk.ToString());&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; customerNotesBO1.Add();&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; customerNotesBO1.cn_Version = 99;&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; customerNotesBO1.Save();&lt;/P&gt;&lt;P&gt;The customersBO1 definitely has a current record yet the save throws the following error:-&lt;/P&gt;&lt;P&gt;"The INSERT statement conflicted with the FOREIGN KEY constraint "Fk_Customers_Notes". The conflict occurred in database "StrataFrameSample", table "dbo.Customers", column 'cust_pk'.&lt;BR&gt;The statement has been terminated."&lt;/P&gt;&lt;P&gt;Am I doing something wrong?&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;Aaron&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Fri, 04 Jul 2008 18:54:42 GMT</pubDate><dc:creator>Aaron Young</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17635.aspx</link><description>Glad it made sense :)</description><pubDate>Fri, 04 Jul 2008 08:49:53 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17632.aspx</link><description>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.&lt;/P&gt;&lt;P&gt;Thanks.</description><pubDate>Fri, 04 Jul 2008 04:30:09 GMT</pubDate><dc:creator>Aaron Young</dc:creator></item><item><title>RE: RegisterForeignKey</title><link>http://forum.strataframe.net/FindPost17626.aspx</link><description>This is not a relationship definition, but a foreign key managment definition.&amp;nbsp; The FillBy methods will still only work if a parent relationship is setup.&amp;nbsp; The RegisterForeignKey methods will automatically propgate and manage foreign key values.&amp;nbsp; 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.&amp;nbsp; When a child record is created, it will automatically update the foreign key field with a -1.&amp;nbsp; 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.&amp;nbsp; This is more akin to a foreign key contraint than a parent relationship definition.</description><pubDate>Thu, 03 Jul 2008 21:45:31 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>