Updating a Foreign Key value using LINQ

Table A has a Foreign Key (integer) field that is referencing a Primary Key (integer) field in Table B. I wanted to be able to set the value of the integer in Table A to a new value using my entity object property for Table A. When I tried to set the value of the integer field I got a ForeignKeyReferenceAlreadyHasValueException and the message ‘Operation is not valid due to the current state of the object.’

The solution is not to try and set the integer value of the field but define the value for the whole entity. For example;

// Dont assign just the ID value

EntityName.ForeignKeyFieldID = myIntegerValue;

// Assign the entity

EntityName.ForeignKeyField = myEntity;