I have been working with LINQ to SQL for a little while now. When I attempt to update a entity property, I get the following exception: “Operation is not valid due to the current state of the object” which gets fired on ForeignKeyReferenceAlreadyHasValueException().
I was trying to assign a value to the foreign key property. There are two properties (references or lists of other objects) and the foreign keys which are used to define this relationship in the database. If foreign key fields and association properties are not equal, LINQ to SQL will throw this exception when you attempt to submit changes. Because it doesn’t know which is the correct value, the one in the foreign key field or the one on the other side of the relationship. The code-generated objects will automatically keep the foreign keys in sync with any association (foreign keys) property when you assign the association property a value.
So the best way to assign the relationship by changing the association property and not the foreign keys. The only time a foreign key field is used as the value is when there is either no association property defined on the object using that field as a key or the association property was never assigned a value.
MSDN document for ForeignKeyReferenceAlreadyHasValueException: