DataRelation and typed DataSet

Stonkie

Well-known member
Joined
Sep 12, 2007
Messages
279
Programming Experience
1-3
This is not so much a question as a reflexion that may help other and maybe someone will enlighten me in the process.

I have a database which's relations do NOT have the cascade update setting. Basically, it is unnecessary because the Ids are auto generated and will never change.

I dragged a few of those on the dataset designer and started coding over this. Then, whenever I created a new record in a parent table and new records in child tables before I saved the record from the parent table, I would save the parent table first, then the child tables.

At first, this seemed to work well, but then I thought I would mess with the auto generated id by creating a a few records in the parent table directly in the database to test the "refresh datatable" option. I didn't refresh the dataset just yet so those changes were visible only to the database and I launched my update function to save the parent and child tables.

I refreshed my data and, to my surprise, the data from the child tables had been assigned to one of the parent record I created manually. Actually, it was saved with the same foreign key id. That means when the parent table refreshed, it loaded its new Id but it did not cascade those changes to the foreign keys related to that record!

After some digging, I found that the relations made by the dataset designer had been created as relations only and cascade update was needed to be able to create child record before the parent record exists in the database.

I set every parent-child DataRelation objects in the dataset designer to "Foreign key constraint and relation" and set the update rule to "Cascade" and my problem was solved.

It seems strange to be forced to do this when the Id column which triggers the update cascade is read-only...

Also, I found that setting the AutoIncrementStep property of my Id columns to -1 would allow me to get exceptions instead of the initial confusing behavior of saving child records in the wrong parent record.

Anyone has a common practice solution else than what I came up with?
 
Back
Top