EFCore基础方法(增,删,查,改,分页),基础泛型类
public class EfDbGet<TEntity> : FilterFunction<TEntity> where TEntity : class
{
public PmsContext _context;
public EfDbGet(PmsContext context)
{
_context = context;
}
//获取所有数据
public virtual List<TEntity> GetListAll()
{
IQueryable<TEntity> ble = _context.Set<TEntity>();
return ble.ToList();
}
//根据id获取单条数据
public virtual TEntity GetById(object id)
{
return _context.Set<TEntity>().Find(id);
}
//按条件查询数据
public virtual List<TEntity> GetBy(Expression<Func<TEntity, bool>> whereLambda)
{
return _context.Set<TEntity>().Where(whereLambda).ToList();
}
//按条件只查询一条数据
public virtual TEntity FindFirst(Expression<Func<TEntity, bool>> whereLambda)
{
IList<TEntity> query = _context.Set<TEntity>().Where(whereLambda).ToList();
if (query != null)
{
return query.FirstOrDefault();
}
else
{
return null;
}
}
// 单条数据添加
public virtual bool Add(TEntity t)
{
_context.Set<TEntity>().Add(t);
if (_context.SaveChanges() > 0)
{
return true;
}
else
{
return false;
}
}
// 批量添加数据
public virtual bool AddRange(IList<TEntity> t)
{
using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
{
try
{
_context.AddRange(t);
int result = _context.SaveChanges();
transaction.Commit();
return true;
}
catch (Exception e)
{
string msg = e.Message;
transaction.Rollback();
return false;
}
}
}
// 根据id删除一条数据
public virtual bool Delete(int id)
{
TEntity obj = _context.Set<TEntity>().Find(id);
_context.Set<TEntity>().Remove(obj);
if (_context.SaveChanges() > 0)
{
return true;
}
else
{
return false;
}
}
// 删除主表以及子表表(DeleteAndDetail<Detail>是一个泛型方法,Detail只是一个占位符,不是一个类)
public virtual bool DeleteAndDetail<Detail>(int id, List<Detail> detail = null) where Detail : class
{
using (IDbContextTransaction begin = _context.Database.BeginTransaction())
{
try
{
TEntity obj = _context.Set<TEntity>().Find(id);
_context.Set<TEntity>().Remove(obj);
if (_context.SaveChanges() > 0)
{
if (detail != null)
{
_context.Set<Detail>().RemoveRange(detail);
_context.SaveChanges();
}
begin.Commit();
return true;
}
else
{
begin.Rollback();
return false;
}
}
catch (Exception e)
{
begin.Rollback();
return false;
}
}
}
// 批量删除数据
public virtual bool DeleteRange(IEnumerable<TEntity> ts)
{
using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
{
try
{
_context.Set<TEntity>().RemoveRange(ts);
_context.SaveChanges();
transaction.Commit();
return true;
}
catch
{
transaction.Rollback();
return false;
}
}
}
// 修改单条数据
public virtual bool Update(TEntity entity)
{
_context.Set<TEntity>().Update(entity);
if (_context.SaveChanges() > 0)
{
return true;
}
else
{
return false;
}
}
// 批量修改数据
public virtual bool UpdateRange(IEnumerable<TEntity> ts)
{
using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
{
try
{
_context.UpdateRange(ts);
_context.SaveChanges();
transaction.Commit();
return true;
}
catch
{
transaction.Rollback();
return false;
}
}
}
//分页查询
public IQueryable<TEntity> GetModelPageData(IQueryable<TEntity> queryable, out int count, out int coungPage, int pageIndex = 0, int pagesize = 0)
{
pageIndex = pageIndex <= 0 ? 1 : pageIndex;
pagesize = pagesize <= 0 ? 10 : pagesize;
count = queryable.Count();//这里需要使用结果,所以需要加载数据(执行.Count()就不是延迟加载了,是真正加载数据了)
if (count % pagesize == 0)
{
coungPage = count / pagesize;//总页数
}
else
{
coungPage = (count / pagesize) + 1;
}
return queryable.Skip((pageIndex - 1) * pagesize)//这里是延迟加载,并不会加载数据
.Take(pagesize);//这里是延迟加载,并不会加载数据
}
}