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);

This entry was posted in 7671, 7674. Bookmark the permalink.

8 Responses to EntityReference change key

  1. En says:

    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.

  2. Matthieu MEZIL says:

    You should look at this.

  3. Tony D says:

    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?

  4. Matthieu MEZIL says:

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

  5. Tony D says:

    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.

  6. Dave Wils says:

    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?

  7. Matthieu MEZIL says:

    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.
    Thanks
    Matthieu

  8. David says:

    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 :(

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>