How to refresh and entity object after a newly created object context

.net, c#, entity-framework

Solution

First attach the entity to the context before refresh,

someObjectContext.Products.Attach(someProduct);

or

someObjectContext.Set<Product>().Attach(someProduct);

That should do it.

Problem

Lets say, i have an `EntityObject` called `someProduct` : ``` //Get the object Product someProduct = someObjectContext.Product.First(); //At runtime at some point, recreate the ObjectContext someObjectContext = new SomeObjectContext(); //Try to refresh someProduct on the new ObjectContext someObjectContext.Refresh(RefreshMode.StoreWins, someProduct); ``` When the third line executes, it throws an exception: The element at index 0 in the collection of objects to refresh has a null EntityKey property value or is not attached to this ObjectStateManager. Is this the correct way to refresh the `EntityObject` on a newly create `ObjectContext`? EDIT: The reason for new `ObjectContext` is to refresh all the dirty `EntityObjects`.

Original source