Question

I am developing some extensions methods to add some funcionalities for DbSet. However, when creating an "Update" method, I need the DbSet's DbContext to be able to modify the state of a entity. The current implementation:

public void Update<TEntity>(this DbSet<TEntity> repository, TEntity entity) where TEntity : class
    {
        repository.Attach(entity);
        var context = // how get the context from repository?
        ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
    }

Does any one know how to get a DbContext from a DbSet instance?

Was it helpful?

Solution

I've found a better way to accomplish that:

public static void MarkAsModified(this DbContext context, object entity)
    {
        context.Entry(entity).State = EntityState.Modified;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top