DEV Community

Cover image for System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException: Operation is not valid due to the current state of the object.
Sandeep Kumar
Sandeep Kumar

Posted on • Edited on

3 2

System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException: Operation is not valid due to the current state of the object.

You might have sometime faced the below exception while attempting to update an entity property:

System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException: Operation is not valid due to the current state of the object.

Reason:

After looking into the issue I found that this error occurs when an attempt is made to change a foreign key when the entity is already loaded.
In this case, I was trying to assign a value to the foreign key property, and an exception is thrown as foreign key fields and association properties don’t match, when changes are submitted.

In such a scenario there are two values, one in the foreign key field or the one on the other side of the relationship and it doesn’t know which value is correct, and as a result exception is thrown.

Solution:

To avoid the exception the best way to update the relationship is by changing the association property and not the foreign keys. And then it will automatically keep the foreign keys in sync when you assign the association property a value.

The foreign key field value is changed only when there is either no association property defined on the object or the association property was never assigned a value.

For Example we have Address object and Country as it's child object, with foreign column is CountryId:

Then if we do

dbContext.Address.CountryId = newCountryId;
Enter fullscreen mode Exit fullscreen mode

it'll raise exception, to avoid this update the relationship by changing the association property as:

dbContext.Address.Country = db.Country.Single(c => c.Id = newCountryId);
Enter fullscreen mode Exit fullscreen mode

Originally published at http://diary-techies.blogspot.com on
March 20, 2014.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay