如何使用Entity Framework 6和我的存储库删除多个记录? [英] How can I use Entity Framework 6 and my repository to delete multiple records?

查看:87
本文介绍了如何使用Entity Framework 6和我的存储库删除多个记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6,我有一个仓库看起来像下面这样,删除了添加和更新方法,使其更短:

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);
    }
}

在我的控制器中,我像这样调用存储库: / p>

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);
        }
    }

这适用于单次测试,但如何删除例如,testId列值为1的所有测试为
,该考试表是Test Table中的列之一。

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.

推荐答案

您可以在通用存储库类中创建另一个删除方法,如下所示:

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);
        }
    }

那么你可以像下面一样使用它,它会删除所有记录 Id 等于 id

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

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

这篇关于如何使用Entity Framework 6和我的存储库删除多个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆