StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      



RegisterForeignKeyExpand / Collapse
Author
Message
Posted 07/03/2008 5:17:40 PM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Yesterday @ 5:29:52 AM
Posts: 177, Visits: 430
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

Post #17624
Posted 07/03/2008 9:45:31 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Today @ 6:26:12 PM
Posts: 4,901, Visits: 4,887
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.
Post #17626
Posted 07/04/2008 4:30:09 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Yesterday @ 5:29:52 AM
Posts: 177, Visits: 430
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.

Post #17632
Posted 07/04/2008 8:49:53 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Today @ 6:26:12 PM
Posts: 4,901, Visits: 4,887
Glad it made sense
Post #17635
Posted 07/04/2008 6:54:42 PM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Yesterday @ 5:29:52 AM
Posts: 177, Visits: 430
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

Post #17636
Posted 07/06/2008 7:01:35 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Today @ 6:26:12 PM
Posts: 4,901, Visits: 4,887
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.
Post #17652
Posted 07/07/2008 5:31:17 AM
StrataFrame User

StrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame UserStrataFrame User

Group: StrataFrame Users
Last Login: Yesterday @ 5:29:52 AM
Posts: 177, Visits: 430
Okay thanks - I appreciate that.

Aaron

Post #17659
Posted 07/07/2008 3:34:25 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Today @ 6:26:12 PM
Posts: 4,901, Visits: 4,887
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();
Post #17690
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Ben Chase, Trent L. Taylor, Steve L. Taylor

PermissionsExpand / Collapse

All times are GMT -6:00, Time now is 8:30pm

Powered by InstantForum.NET v4.1.4 © 2009
Execution: 0.094. 10 queries. Compression Enabled.
Site Map - Home - My Account - Forum - About Us - Contact Us - Try It - Buy It

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.