Mocking or faking DbEntityEntry or creating a new DbEntityEntry

Just like the other case, what you need is to add an additional level of indirection:

interface ISalesContext
{
    IDbSet<T> GetIDbSet<T>();
    void SetModified(object entity)
}

class SalesContext : DbContext, ISalesContext
{
    public IDbSet<T> GetIDbSet<T>()
    {
        return Set<T>();
    }

    public void SetModified(object entity)
    {
        Entry(entity).State = EntityState.Modified;
    }
}

So, instead of calling the implementation, you just call SetModified.

Leave a Comment