How can I use Entity Framework 6 and my repository to delete multiple records?

asp.net-mvc entity-framework entity-framework-6

Question

I am using Entity Framework 6 and I have a repository looking like the following with the Add and Update methods removed to make it shorter:

 public class GenericRepository<T> : IRepository<T> where T : class
{
    public GenericRepository(DbContext dbContext)
    {
        if (dbContext == null) 
            throw new ArgumentNullException("An instance of DbContext is required to use this repository", "context");
        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }

    protected DbContext DbContext { get; set; }

    protected DbSet<T> DbSet { get; set; }

    public virtual IQueryable<T> Find(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where<T>(predicate);
    }

    public virtual IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public virtual T GetById(int id)
    {
        //return DbSet.FirstOrDefault(PredicateBuilder.GetByIdPredicate<T>(id));
        return DbSet.Find(id);
    }

    public virtual void Delete(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Deleted)
        {
            dbEntityEntry.State = EntityState.Deleted;
        }
        else
        {
            DbSet.Attach(entity);
            DbSet.Remove(entity);
        }
    }

    public virtual void Delete(int id)
    {
        var entity = GetById(id);
        if (entity == null) return; // not found; assume already deleted.
        Delete(entity);
    }
}

In my controller I call the repository like this:

    public HttpResponseMessage DeleteTest(int id)
    {
        Test test = _uow.Tests.GetById(id);
        if (test == null)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        try
        {
            _uow.Tests.Delete(test);
            _uow.Commit();
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }

This works for a single test but how can I delete for example all tests that have an examId column value of 1 being that examId is one of the columns in the Test table.

Popular Answer

You can create another delete method in your generic repository class, see below:

    public virtual void Delete(Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> query = DbSet.Where(predicate).AsQueryable();
        foreach (T obj in query)
        {
            DbSet.Remove(obj);
        }
    }

Then you can use it like below, it will delete all records which Id equalsid.

  _uow.Test.Delete(n => n.Id = id)



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why