Removing the Detach method from the API ( DbContext ) has some logic, since Detach does not work on the object graph, but only separates the only object that you pass to the method. This is different from all other methods that change the state of an object:
Attach attaches the supplied object, including all related objects in the object graph of the navigation properties.Add adds the supplied object, including all related objects to the context.Remove removes the provided object, including related objects that were configured using cascading deletion
On the other hand, manual installation of Modified , Added or Deleted manually always affects only the provided object, and not related objects. This also applies to calling the Detach ObjectContext method. Rather, it is connected with detaching an object only by setting the state to Detached in accordance with the behavior of other state changes, because, as in the case of any other state, it only affects the provided object without associated objects.
DbContext is among other features designed to simplify working with the Entity Framework. The old Detach method was more confusing, and its behavior was unlike the expectations of many developers. ( Here and here are two links about this confusion and the difficulties associated with detaching an object.) In my opinion, this was not the wrong step to remove it from the DbContext API.
Well, you can always write your own extension method, just like you, or access the underlying ObjectContext through an adapter if you really want to have a Detach method.
Slauma
source share