Strange Foreign-Key Exception

May 24, 2011 at 3:16 PM
Edited May 24, 2011 at 3:17 PM

I have encountered an exception that I cannot determine the cause yet.  Still Looking though.  Any help would be appreciated.

Background: Using the NHDAL

I have a combo-box filled with the items from a database table.

I collect some additional information, one more combo-box and a check box.  When the user clicks "Save" I add a new item to a collection.

This "Save" happens correctly for the first time, but after that fails with the following exception: "System.Data.InvalidConstraintException: ForeignKeyConstraint ClientsApplicationClients_ClientsApplicationClientsRelation requires the child key values (5) to exist in the parent table."

Here is the relevant snippet;


 Dim collection As ApplicationClientsCollection = _data.ParentCollection.SubDomain.GetCollection(Of ApplicationClientsCollection)()           

 Dim newItem As ApplicationClients = collection.NewItem(ApplicationID, ddlOtherClients.SelectedValue)           

With newItem               

.MovingIn = chkMovingIn.Checked               

.RelationshipID = ddlRelationship.SelectedValue               

.IsApplicant = False               

.IsCoApplicant = False           

End With          



I would appreciate whatever assistance I can get here!



May 24, 2011 at 5:16 PM
Edited May 24, 2011 at 5:21 PM

It's almost like its lazyloading (I don't know how else to describe it) only the first time you add a clientID.  Because after that the "Clients" table is in the collections subdomain, but contains only one row, while the database contains them all.


I don't know if that makes any sense.


May 25, 2011 at 3:44 PM

This issue does not seem to occur when the ClientID is not part of the primary-key of the new item (just wrote similar code for a different section).

Even more confused,



May 25, 2011 at 4:13 PM

One this I do notice is that you load the client collection in the same subdomain container as your data object. Now the integrity is maintained in memory. So if you add a client ID to the data object (or any other FK), it must already exist in the collections that are also loaded in the subdomain or the referential integrity will be violated (per the error you get). I cannot all of your code or what is already loaded in the data objects subdomain. However keep in mind that the subdomain is a little database in memory and if you have multiple tables (collections) loaded then their integrity MUST be kept.

If this does not help then please send the model and a sample project with instructions to break it to I would like to get to the bottom of this. I need to see your relationships and such.

May 25, 2011 at 4:24 PM

This seems to be what I was thinking the problem was too.  Question is, how do I ensure that the Client Exists in the Client Table held in the subdomain and if its not load it?




May 25, 2011 at 5:09 PM

Optionally, is there a way to force the subdomain to return to the database to look for the ClientID and grab that client to load into the clientcollection each time, and not just the first time when no Client Table exists yet in the subdomain.

I guess the confusing part for me here, is that the subdomain is willing to check the database for the first clientID added to this collection, but not for any additional ones.


May 25, 2011 at 7:24 PM

My next thought was to verify that the client is in the collection and if not grab the client and merge it into the subdomain.  This seems to partially work, removing the original error and replacing it with a deeper relation error.  (a relation of the client table).  I am somewhat hesitant to go down this rabbit hole.  Is there a simpler way to do what I am attempting without resorting to persisting any information until I am ready to persist my overriding object "_data"?

If (From t In _data.ParentCollection.SubDomain.GetCollection(Of ClientsCollection)() Where t.ClientID = ddlOtherClients.SelectedValue).Count <= 0 Then  

   _data.ParentCollection.SubDomain.Merge(CType(ClientsCollection.SelectUsingPK(ddlOtherClients.SelectedValue), Clients).ParentCollection.SubDomain, True, MissingSchemaAction.AddWithKey)

End If