Search code examples
entity-frameworkcode-first

Entity Framework Code First - No Detach() method on DbContext


I'm wondering why there is no Detach method on the DbContext object like there is for ObjectContext.  I can only assume this omission was intentional, but I have a hard time figuring out why.  I need to be able to detach and re-attach entities (for putting in the cache in an ASP.NET project, for example).  However, since I can't detach an entity, when I try to attach an entity that was associated with a previous context, I get the "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" exception.

What's the guidance here?  Am I missing something?


Solution

  • For people that might stumble upon this question, as of CTP5 you now need to write

    ((IObjectContextAdapter)context).ObjectContext
    

    in order to get to ObjectContext.