ForeignKey Bug In the DataGrid

Apr 10, 2009 at 2:24 PM

We have 3 Tables:
Main Table Customers
Child Table Orders
GrandChild Table OrderDetails
There are Foreign Relationships between the Customers and Orders Table and Between the Oders and OrderDetails Table

We have a ListBox whose ItemsSource is Customers i.e. The Main Table
We have a DataGrid whose ItesSource has been set to the Relation Between Customers and Orders i.e. FK_Orders_Customers
We have another DataGrid whose ItesSource has been set to the Relation Between Orders and OrderDetails i.e. FK_Orders_Customers/FK_OrderDetails_Orders

if the Order table record is saved, the foreign key gets generated correctly when adding a new record to the OrderDetails Table using the NewItemRow in the DataGrid
But if the record in the Order Table is a new record (i.e. not yet saved), the foregin key when addinmg a New Orderdetails record is not generated correctly and you get an Exception that first displays

There is no source code available for the current location.


ForeignKeyConstraint FK_OrderDetails_Orders requires the child key values (-1) to exist in the parent table.

Even after this operation, now that the Orders table has been saved and its actual identity has been retreived from the Database, if you re-try to add the OrderDetails record again, you still get the -1 as the

foreign key. If you close the application and restart it, and then add a new OrderDetails record, then you get the correct foreign key.
Any ideas of how to solve this problem?