Reputation: 9476
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
.
Upvotes: 5
Views: 5859
Reputation: 162
You should attach the some product with object, then only you can refresh the object.
This refresh is used for refreshing the previously created cache related to object content.
I have used the following code in project. It working fine.
objectContext.attach(someProduct);
Upvotes: 0
Reputation: 4628
Since the someProduct was retrieved using a different ObjectContext you need to use someObjectContext.Attach(someProduct)
.
The refresh is used to refresh the cache of the ObjectContext, It needs an entity selected from the same Object Context.
Good luck
Upvotes: 1
Reputation: 4803
First attach the entity to the context before refresh,
someObjectContext.Products.Attach(someProduct);
or
someObjectContext.Set<Product>().Attach(someProduct);
That should do it.
Upvotes: 3