0

I sometimes get The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Is there any way to renew a ObjectContext for a specific entity?

(one that stayed in my application from previous request (i.e. my caching layer))

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 1
    why don't you create a new instance of object context and fetch the entity from DB again? – daryal Feb 05 '13 at 08:42
  • Did you manage your dbContext as per-request? – cuongle Feb 05 '13 at 08:44
  • @daryal is rigth. in your scenerio, ObjectContext disposing have no meaning. Check this question: http://stackoverflow.com/questions/14643491/drawbacks-with-using-entity-framework-without-using-statement – AliRıza Adıyahşi Feb 05 '13 at 08:47
  • What is exactly your scenario? Are you using EF behind a services layer? If so, you should create a new instance of the context for each request. – Rui Jarimba Feb 05 '13 at 10:25
  • @CuongLe I create an new instance per request, but I also save the result in an enterprise librrary cache, as I mentioned in my question – Elad Benda Feb 05 '13 at 12:25
  • @daryal I create an new instance per request, but I also save the result in an enterprise librrary cache, as I mentioned in my question – Elad Benda Feb 05 '13 at 12:26
  • @EladBenda instead of renewing context, I would advise to detach the object from context after using it, or before closing the service call, then reattaching it to a new context. – daryal Feb 05 '13 at 12:37
  • how do I detach or reattach? – Elad Benda Feb 05 '13 at 12:44

0 Answers0