views:

165

answers:

1

I have an entity with a status property that I would like to update.

I would like to do the following:

const int NEW_STATUS = 2;
myEntity.StatusReference.EntityKey = new EntityKey("SetName", "KeyName", NEW_STATUS);

When this is passed to the context, its state is "UnChanged", despite me changing the relationship! This means the save will not be persisted.

The entity comming in is from a different context to the one that its being attached to and saved.

Anyone know how I can update just the entitykey and persist it!?

Thanks in advance,

David

A: 

You can't. EntityKeys are designed to be mapped to primary keys, which, in any good DB design, will never change. If you've mapped your EntityKey to something which is not a PK, change it to the PK. If your DB design calls for PKs to change, please reconsider that design. (Removed after you changed the question.)

Added, upon re-reading the question: Are you actually wanting to update the EntityKey of the entity, or do you just want to change the status property? If the latter, try one of the following:

entity.Status = someStatusInstance;

...or...

entity.StatusReference.EntityKey = myEntity.EntityKey = new EntityKey("SetName", "KeyName", NEW_STATUS);

If your entity state isn't modified, you probably have the order of operations wrong when adding to/saving the context. You would need to show that when asking for help on it.

Craig Stuntz
Sorry, ive updated my post (typo). Its an FK not PK! :)
David Kiff
The latter, I tried "entity.Status = someStatusInstance", however that doesnt work due to the entity being in a different context from the one where I retrived the status from! I know the value for the FK I want to update, so dont need the round trip either :)
David Kiff
Not sure the ordering is wrong.. seems like a common problem, however I cant seem to find a suitable solution! This guy has a simular problem... http://stackoverflow.com/questions/1178885/update-entitykey-reference-in-entity-framework
David Kiff
Believe me, it's wrong. We do this *all the time* and I can guarantee you it works. I will add that using multiple contexts concurrently is going to confuse even EF experts. Avoid it whenever possible.
Craig Stuntz
Its passing the entities through layers, and I didnt want to use my context as a singleton! I want something like "ApplyProperties" however it doesnt work on entityRelationships http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/87f63056-7309-41af-8be7-313c20594bde/
David Kiff
And there is not much to get wrong on the ordering... pull entity from the database, update just the entitykey for one of the FK's and call savechanges...
David Kiff
I'm not saying you *can'* use multiple contexts. It does work, but expect it to be more confusing than using just one. There is quite a bit you need to do in just the right order which you don't need to do with only one context.
Craig Stuntz
Took your advice, and used a single context for that particular set of operations :) thanks..
David Kiff