As explained in this topic if you like to insert or update an entity you can use the following approach:
if (myEntity.Id != 0)
{
context.MyEntities.Attach(myEntity);
context.ObjectStateManager.ChangeObjectState(myEntity, EntityState.Modified);
}
else
{
context.MyEntities.AddObject(myEntity);
}
context.SaveChanges();
The problem is that in my case myEntity has some child related objects and therefore when I try to update it (myEntity.Id != 0) I receive the following exception:
System.InvalidOperationException was unhandled
Message=An object with a temporary EntityKey value cannot be attached to an object context.
If I remove the child objects (comment out) from myEntity everything works..
Does anybody know how to solve the problem? How can I update myEntity and save it with its child objects? THANKS!
EDIT:
Here the solution to the problem: stackoverflow.com/a/7969372/282649