C# EF 数据访问基础类
接口类
/// <summary> /// 数据库模型处理服务基础接口 /// </summary> public interface IDbServiceBase { /// <summary> /// 取得数据库模型上下文 /// </summary> /// <returns></returns> DbContext DbContext { get; } /// <summary> /// 设置对象实体 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> DbSet<T> DbSet<T>() where T : class; /// <summary> /// 通过ID取得数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> T? Find<T>(object id) where T : class; /// <summary> /// 通过ID取得数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> Task<T?> FindAsync<T>(object id) where T : class; /// <summary> /// 取得数据列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereLambda"></param> /// <returns></returns> List<T> GetList<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class; /// <summary> /// 取得数据列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereLambda"></param> /// <returns></returns> Task<List<T>> GetListAsync<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class; /// <summary> /// 取得数据记录数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> /// <returns></returns> int GetCount<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class; /// <summary> /// 取得数据记录数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> /// <returns></returns> Task<int> GetCountAsync<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class; /// <summary> /// 判断数据是否存在 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> /// <returns></returns> bool GetAny<T>(Expression<Func<T, bool>> whereLambda) where T : class; /// <summary> /// 判断数据是否存在 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> /// <returns></returns> Task<bool> GetAnyAsync<T>(Expression<Func<T, bool>> whereLambda) where T : class; /// <summary> /// 添加数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> void Insert<T>(T entity, bool saveFlag = false) where T : class; /// <summary> /// 添加数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> Task InsertAsync<T>(T entity, bool saveFlag = false) where T : class; /// <summary> /// 修改数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> void Update<T>(T entity, bool saveFlag = false) where T : class; /// <summary> /// 修改数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> Task UpdateAsync<T>(T entity, bool saveFlag = false) where T : class; /// <summary> /// 删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> void Delete<T>(T entity, bool saveFlag = false) where T : class; /// <summary> /// 删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> Task DeleteAsync<T>(T entity, bool saveFlag = false) where T : class; /// <summary> /// 保存修改 /// </summary> void Save(); /// <summary> /// 保存修改 /// </summary> Task SaveAsync(); /// <summary> /// 取得分页数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageSize"></param> /// <param name="pageNum"></param> /// <param name="whereLambda"></param> /// <returns></returns> PageList<T> GetPageData<T>(int pageSize, int pageNum, Expression<Func<T, bool>>? whereLambda = null) where T : class; /// <summary> /// 取得分页数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageSize"></param> /// <param name="pageNum"></param> /// <param name="whereLambda"></param> /// <returns></returns> Task<PageList<T>> GetPageDataAsync<T>(int pageSize, int pageNum, Expression<Func<T, bool>>? whereLambda = null) where T : class; }
实现类
/// <summary> /// 数据库模型处理服务基础接口实现 /// </summary> public class DbServiceBase : IDbServiceBase { /// <summary> /// 数据库上下文 /// </summary> protected readonly DbContext _context; /// <summary> /// 构造函数 /// </summary> /// <param name="context"></param> public DbServiceBase(DbContext context) { _context = context; } /// <summary> /// 数据库上下文 /// </summary> /// <returns></returns> public DbContext DbContext { get { return _context; } } /// <summary> /// 设置对象实体 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public DbSet<T> DbSet<T>() where T : class { return _context.Set<T>(); } /// <summary> /// 通过ID取得数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public T? Find<T>(object id) where T : class { return _context.Set<T>().Find(id); } /// <summary> /// 通过ID取得数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public async Task<T?> FindAsync<T>(object id) where T : class { return await _context.Set<T>().FindAsync(id); } /// <summary> /// 取得数据列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereLambda"></param> /// <returns></returns> public List<T> GetList<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class { if (whereLambda != null) { return _context.Set<T>().Where(whereLambda).ToList(); } else { return _context.Set<T>().ToList(); } } /// <summary> /// 取得数据列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereLambda"></param> /// <returns></returns> public async Task<List<T>> GetListAsync<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class { if (whereLambda != null) { return await _context.Set<T>().Where(whereLambda).ToListAsync(); } else { return await _context.Set<T>().ToListAsync(); } } /// <summary> /// 取得数据记录数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> /// <returns></returns> public int GetCount<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class { if (whereLambda != null) { return _context.Set<T>().Where(whereLambda).Count(); } else { return _context.Set<T>().Count(); } } /// <summary> /// 取得数据记录数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> /// <returns></returns> public async Task<int> GetCountAsync<T>(Expression<Func<T, bool>>? whereLambda = null) where T : class { if (whereLambda != null) { return await _context.Set<T>().Where(whereLambda).CountAsync(); } else { return await _context.Set<T>().CountAsync(); } } /// <summary> /// 判断数据是否存在 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereLambda"></param> /// <returns></returns> public bool GetAny<T>(Expression<Func<T, bool>> whereLambda) where T : class { return _context.Set<T>().Any(whereLambda); } /// <summary> /// 判断数据是否存在 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="whereLambda"></param> /// <returns></returns> public async Task<bool> GetAnyAsync<T>(Expression<Func<T, bool>> whereLambda) where T : class { return await _context.Set<T>().AnyAsync(whereLambda); } /// <summary> /// 添加数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> public void Insert<T>(T entity, bool saveFlag = false) where T : class { _context.Add<T>(entity); if (saveFlag) _context.SaveChanges(); } /// <summary> /// 添加数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> public async Task InsertAsync<T>(T entity, bool saveFlag = false) where T : class { _context.Add<T>(entity); if (saveFlag) await _context.SaveChangesAsync(); } /// <summary> /// 修改数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> public void Update<T>(T entity, bool saveFlag = false) where T : class { _context.Entry(entity).State = EntityState.Modified; if (saveFlag) _context.SaveChanges(); } /// <summary> /// 修改数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> public async Task UpdateAsync<T>(T entity, bool saveFlag = false) where T : class { _context.Entry(entity).State = EntityState.Modified; if (saveFlag) await _context.SaveChangesAsync(); } /// <summary> /// 删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> public void Delete<T>(T entity, bool saveFlag = false) where T : class { _context.Entry(entity).State = EntityState.Deleted; if (saveFlag) _context.SaveChanges(); } /// <summary> /// 删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="saveFlag"></param> public async Task DeleteAsync<T>(T entity, bool saveFlag = false) where T : class { _context.Entry(entity).State = EntityState.Deleted; if (saveFlag) await _context.SaveChangesAsync(); } /// <summary> /// 保存修改 /// </summary> public void Save() { _context.SaveChangesAsync(); } /// <summary> /// 保存修改 /// </summary> public async Task SaveAsync() { await _context.SaveChangesAsync(); } /// <summary> /// 取得分页数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageSize"></param> /// <param name="pageNum"></param> /// <param name="whereLambda"></param> /// <returns></returns> public PageList<T> GetPageData<T>(int pageSize, int pageNum, Expression<Func<T, bool>>? whereLambda = null) where T : class { if (whereLambda == null) { var totalCount = _context.Set<T>().Count(); var list = _context.Set<T>().Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList(); return new PageList<T>(pageSize, pageNum, totalCount, list); } else { var totalCount = _context.Set<T>().Where(whereLambda).Count(); var list = _context.Set<T>().Where(whereLambda).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList(); return new PageList<T>(pageSize, pageNum, totalCount, list); } } /// <summary> /// 取得分页数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageSize"></param> /// <param name="pageNum"></param> /// <param name="whereLambda"></param> /// <returns></returns> public async Task<PageList<T>> GetPageDataAsync<T>(int pageSize, int pageNum, Expression<Func<T, bool>>? whereLambda = null) where T : class { if (whereLambda == null) { var totalCount = await _context.Set<T>().CountAsync(); var list = await _context.Set<T>().Skip(pageSize * (pageNum - 1)).Take(pageSize).ToListAsync(); return new PageList<T>(pageSize, pageNum, totalCount, list); } else { var totalCount = await _context.Set<T>().Where(whereLambda).CountAsync(); var list = await _context.Set<T>().Where(whereLambda).Skip(pageSize * (pageNum - 1)).Take(pageSize).ToListAsync(); return new PageList<T>(pageSize, pageNum, totalCount, list); } } }
分页类
/// <summary> /// 分页数据集合 /// </summary> /// <typeparam name="T">对象</typeparam> public class PageList<T> { /// <summary> /// 每页记录数 /// </summary> public int PageSize { get; set; } /// <summary> /// 页码 /// </summary> public int PageNum { get; set; } /// <summary> /// 总页数 /// </summary> public int PageCount { get { return TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1; } } /// <summary> /// 总记录数 /// </summary> public int TotalCount { get; set; } /// <summary> /// 该页数据 /// </summary> public List<T>? DataList { get; set; } /// <summary> /// 构造函数 /// </summary> public PageList() { } /// <summary> /// 构造函数 /// </summary> /// <param name="pageSize"></param> /// <param name="pageNum"></param> /// <param name="totalCount"></param> /// <param name="dataList"></param> public PageList(int pageSize, int pageNum, int totalCount, List<T>? dataList) { PageSize = pageSize; PageNum = pageNum; TotalCount = totalCount; DataList = dataList; } }