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

后端 未结 4 2010
北恋
北恋 2020-11-27 03:42

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 tim

相关标签:
4条回答
  • 2020-11-27 04:11

    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 "Detach is too advanced technology and should be hidden". IMHO the man who invented this should be shot - because if you add brand new entity, it is otherwise difficult to just remove it without commiting changes to db (you can manipulate with DbEntityEntry but that's another story).

    Edit 4 years later:

    With EF6 (i somehow skipped EF5 :) ) you dont need detach() anymore, becouse removing freshly added entry does not generate delete from [table] where [Id] = 0 as in EF4 - you can just call mySet.Remove(myFreshlyCreatedAndAddedEntity) and everything will be allright.

    0 讨论(0)
  • 2020-11-27 04:18

    DbContext uses an ObjectContext internally and EF team make this available as a protected property just in case you ever need to drop down to the lower level API and sounds like this is the case here, so you can use or expose the required functionality from a derived DbContext:

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

    Then you can call this method from your controller to detach an entity.

    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 is how DbContext looks like from metadata:

    public class DbContext : IDisposable 
    {      
        protected System.Data.Objects.ObjectContext ObjectContext { get; }
        ...
    }
    
    0 讨论(0)
  • 2020-11-27 04:23

    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.

    0 讨论(0)
  • 2020-11-27 04:28

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

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

    later you can use this property for variety of useful stuff ... like Detach :)

    0 讨论(0)
提交回复
热议问题