EntityFramework Long Running ObjectContext: How to detect data source changes not tracked by the ObjectContext
During the life of my ObjectContext, I receive a message saying telling me that a new entity has been added to the data source by some other process (i.e. not tracked by my ObjectContext). How do I add this entity to my local ObjectContext without recreating it?
I've looked at ObjectContext.Refresh()
but I'开发者_StackOverflow中文版m not sure if this is the right way forwards.
If the entity is available in your code, use the Attach method.
In case this object is not available in your code, the solution is indeed to call the Refresh method with StoreWins RefreshMode for the collection the object was added into.
I recommend you to take a look at the Self-Tracking Entities as well.
Is the answer not in the question title - long running object contexts are not a good idea in EF. Ideally the OC should be kept alive for as short a time as possible.
精彩评论