Entity Framework Code First - No Detach () Method in DbContext

I am wondering why there is no Detach method for a DbContext object, as for an ObjectContext. I can only assume that this omission was intentional, but it is difficult for me to understand why. I need to be able to detach and reattach objects (e.g. for clustering in an ASP.NET project). However, since I cannot detach the object, when I try to attach an object that was connected to the previous context, I get "An entity object cannot refer to multiple instances of the IEntityChangeTracker exception."

What is the guide here? Did I miss something?

+62
entity-framework code-first
Nov 12 '10 at 18:55
source share
4 answers

For people who might stumble upon this question, with CTP5 you now need to write

((IObjectContextAdapter)context).ObjectContext 

to go to the ObjectContext.

+85
Dec 08 '10 at
source share

DbContext uses the ObjectContext object internally, and the EF command makes it available as a protected property in case you ever need to go to the lower level API and sounds like it does here, so you can use or expand the required functions from the derived Dbcontext:

 public class YourContext : DbContext { public void Detach(object entity) { ObjectContext.Detach(entity); } } 

You can then call this method from your controller to detach the object.

Alternatively, you can change it to even have a richer API:

 public class YourContext : DbContext { public void ChangeObjectState(object entity, EntityState entityState) { ObjectContext.ObjectStateManager.ChangeObjectState(entity, entityState); } } 

Here's what the DbContext from the metadata looks like:

 public class DbContext : IDisposable { protected System.Data.Objects.ObjectContext ObjectContext { get; } ... } 
+38
Nov 12 '10 at 19:36
source share

EF: CF 4.1 RC1 and EF: CF 4.1 RTW have the same explicitly implemented IObjectContextAdapter:

 public static class DbContextExtensions { public static void Detach(this System.Data.Entity.DbContext context, object entity) { ((System.Data.Entity.Infrastructure.IObjectContextAdapter)context).ObjectContext.Detach(entity); } } 

Microsoft decided that "Detach is too advanced technology and should be hidden." IMHO the person who invented this should be shot, because if you add a new object, otherwise just delete it without making any changes to db (you can manipulate using DbEntityEntry, but that's another story).

Edit 4 years later:

With EF6 (I somehow skipped EF5 :)) you no longer need detach() , because deleting the record you just added doesn't generate delete from [table] where [Id] = 0 , like in EF4 - you can just call mySet.Remove(myFreshlyCreatedAndAddedEntity) and everything will be fine.

+15
May 31 '11 at 11:54 a.m.
source share

I usually extend the base class (inherits from DbContext) with the property:

 public class MyDbContext : DbContext { public ObjectContext ThisObjectContext { get { return ((IObjectContextAdapter)this).ObjectContext; } } } 

later you can use this property for many useful things ... for example, Detach :)

+6
Feb 19 '14 at 7:26
source share



All Articles