8

There is no Detach(object entity) on the DbContext in Entity Framework 5.

To detach an entity, the state now needs to be changed. Maybe I am missing something, but this seems much less intuitive and readable than using the Detach method:

context.Entry(myEntity).State = EntityState.Detached;

I am tempted to just create an extension method to bring back the Detach method:

public static void Detach(this MyEntities context, object entity)
{
    context.Entry(entity).State = EntityState.Detached;
}

What is the reason why Microsoft removed the DbContext.Detach() method in EF 5?

Dave New
  • 38,496
  • 59
  • 215
  • 394
  • 1
    "I am tempted to just create an extension method" -- Why would you create an extension method for a class you have full control over? You can add it to `WIMTEntities` directly. –  May 22 '13 at 11:29

2 Answers2

16

Removing a Detach method from the (DbContext) API has some logic because Detach doesn't operate on an object graph but it only detaches the single object that you pass into the method. This is different to all other methods that change the object state:

  • Attach attaches the supplied object including all related objects in the object graph of navigation properties
  • Add adds the supplied object including all related objects to the context
  • Remove deletes the supplied object including the related objects that have been configured with cascading delete

On the other hand setting the state manually to Modified, Added or Deleted always only acts on the supplied object, not on the related objects. This is also the case for calling the Detach method of ObjectContext. It is more consequent to detach an object only via setting the state to Detached to be in line with the behaviour of other state changes because like setting any other state it only influences the supplied object without related objects.

DbContext is - among other features - intended to make working with Entity Framework easier. The old Detach method was more confusing and its behaviour is not like many developers expect. (Here and here are two references about this confusion and the complexities involved in detaching an object.) In my opinion it wasn't the wrong step to remove it from the DbContext API.

Well, you can always write you own extension method like you did or access the underlying ObjectContext via the adapter if you really want to have a Detach method.

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420
5

I've no idea why there is no Detach() method but the DbSet class exposes a method AsNoTracking() to fetch objects that are detached from the DbContext.

Here's some example code from here

using (var context = new UnicornsContext())
{
    // Query for all unicorns without tracking them
    var unicorns1 = context.Unicorns.AsNoTracking();

    // Query for some unitcorns without tracking them
    var unicorns2 = context.Unicorns
        .Where(u => u.Name.EndsWith("ky"))
        .AsNoTracking()
        .ToList();
} 
qujck
  • 14,388
  • 4
  • 45
  • 74
  • Noting [this answer](http://stackoverflow.com/a/5600202/538763) indicates that AsNoTracking may not be equivalent to Detach. – crokusek Oct 26 '15 at 17:59