Frage

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?

War es hilfreich?

Lösung

I've found a better way to accomplish that:

public static void MarkAsModified(this DbContext context, object entity)
    {
        context.Entry(entity).State = EntityState.Modified;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top