4

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?

Arthur Nunes
  • 6,718
  • 7
  • 33
  • 46
  • 1
    Possible duplicate of [Can you get the DbContext from a DbSet?](http://stackoverflow.com/questions/17710769/can-you-get-the-dbcontext-from-a-dbset) – Michael Freidgeim Jul 13 '16 at 02:59

1 Answers1

0

I've found a better way to accomplish that:

public static void MarkAsModified(this DbContext context, object entity)
    {
        context.Entry(entity).State = EntityState.Modified;
    }
Arthur Nunes
  • 6,718
  • 7
  • 33
  • 46