Tag Archives: update

Linq Update FK – Operation is not valid due to the current state of the object

sql serverTo update the entity in question you need to get a new entity for it from the context rather than setting it directly.

Wrong Way

OrderRecord.customerId = 105;

Throws an error!
“Operation is not valid due to the current state of the object”

Solution

OrderRecord.tblCustomer = db.tblCustomers.Single(t => t.customerID == 105);

Returns no error!