如何使用实体框架6和我的仓库删除多条记录? [英] How can I use Entity Framework 6 and my repository to delete multiple records?

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

问题描述

我使用实体框架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);
    }
}

在我的控制器我叫库是这样的:

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

这适用于一个单一的测试,但我怎么能删除,例如具有1存在的examId列值的所有测试
这examId是在测试表中的一列。

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.

推荐答案

我不知道,如果是EF能够处理多个删除现在给予一定的价值,但我这样做是我最后一次不得不求助于一个循环

I'm not sure if EF is able to handle multiple delete now given a certain value, but the last time I did this I had to resort to a loop.

public HttpResponseMessage DeleteTest(int id)
{
  var testList = _uow.Tests.GetAll().Where(o => o.Id == id);
  if (testList.Count() == 0)
  {
      return Request.CreateResponse(HttpStatusCode.NotFound);
  }
  try
  {
      foreach (var test in testList)
      {
        _uow.Tests.Delete(test);
      }
      _uow.Commit();
      return Request.CreateResponse(HttpStatusCode.OK);
  }
  catch (Exception ex)
  {
      return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
  }
}

如果测试表是外国表挂在ID列中的主表,你可能要考虑做一个层叠在这种情况下删除。

If the "test" table is a foreign table linked to a primary table on the "ID" column, you may want to consider doing a cascading delete in this case.

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

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