EntityReference change key

From Northwind, I want to add the following method to Product class:

public void SetCategory(int categoryID)


    // ???


The problem is that from product class, I have no reference to the ObjectContext (which can be null if the product isn't attached). So it isn't possible to use Category property because we will have an exception if we set this property with a category attached to a different context than the product.

The idea is to use CategoryReference property. CategoryReference is of type System.Data.Objects.DataClasses.EntityReference<Categories> which contains a property EntityKey. We just need to change the EntityKey.

EntityKey has a property EntityKeyValues of type System.Data.EntityKeyMember[]. In our case, the key of Category is only the CategoryID. So we can be tempted to do this:

p.CategoryReference.EntityKey.EntityKeyValues[0].Value = categoryID;

but it isn't possible! Indeed EntityKeyMember is immutable, so you can't change its property Key or Value if they are affected.

So, the solution is to reinstanciate a new EntityKey:

CategoryReference.EntityKey = new EntityKey("NorthwindEntities.Categories", "CategoryID", categoryID);

Published Fri, Jul 25 2008 4:05 by Matthieu MEZIL


# re: EntityReference change key

Have you found a nice way to do this with detached entities?

ApplyPropertyChanges ignores changes to EntityReferences.

Im trying to find a way (without reflection) to update FK property in a detached entity along with other scalar proprties.

Thursday, August 28, 2008 12:19 AM by En

# re: EntityReference change key

You should look at this.

Thursday, August 28, 2008 2:21 AM by Matthieu MEZIL

# re: EntityReference change key

Have you noticed that when you change an EntityReference this way that the object remains in the EntityState.Unchanged state?

If so, have you found a way to get around that?

Wednesday, December 03, 2008 7:34 AM by Tony D

# re: EntityReference change key

Your entity is Unchanged but you have also a Added relationship and a Deleted relationship.

Wednesday, December 03, 2008 3:33 PM by Matthieu MEZIL

# re: EntityReference change key

Ah, I see, so that will still be saved even though the object is not "modified", and I will be able to audit that on the ObjectStateManager.

Thank you.

Wednesday, December 03, 2008 3:52 PM by Tony D

# re: EntityReference change key

This method of updating the old entitykey from the new entitykey (as in ApplyReferencePropertyChanges) does not work. With that I mean that the keys are indeed updated but the changes are not persisted to the database when calling SaveChanges. As a few people have found out already and reported on several websites.

I think the problem is indeed that there needs to be an added and deleted relationship.

My question is how do tell the objectstatemanager that there is a deleted and added relationship using only the entityreference/key?

Tuesday, January 13, 2009 3:45 PM by Dave Wils

# re: EntityReference change key

Hi Dave,

I don't understand your post. Indeed, I try my code and yes the foreign key modification is persisted into the DB when I call SaveChanges.

Can you send me your sample at matthieu.mezil at winwise.fr.



Tuesday, January 20, 2009 1:17 PM by Matthieu MEZIL

# re: EntityReference change key

Dave is right.  

If you do what you have done, and call SaveChanges on the newly changed object, its relationship will not be updated.

Changes to the EntityKey do not seem to change the EntityState from Unchanged :(

Tuesday, September 08, 2009 6:54 AM by David

Leave a Comment

If you can't read this number refresh your screen
Enter the numbers above: