时间:2021-07-01 10:21:17 帮助过:15人阅读
数据存储层在项目Ninesky.DataLibrary中实现,整个项目只有一个类Repository。
Repository中实现增删改查询等方法供业务逻辑层调用,主要功能如下图:
具体步骤
一、添加实体框架的引用
1、打开解决方案,选择项目Ninesky.DataLibrary,在引用上右键,选择管理NuGet程序包。
在NuGet包管理器中的浏览标签中点击EntityFramework,点击右侧栏的安装按钮。
在搜索框输入EntityFramework.zh-Hans,安装假体中文资源包。
二、实现数据仓储类
打开解决方案,选择项目Ninesky.DataLibrary,将Class1.cs重命名为Repository.cs,在文档头添加using System.Data.Entity;将类名重命名为public class Repository。改为public class Repository<T> where T :class
1、DbContext属性
在Repository类中添加如下代码 public DbContext DbContext { get; set; }
2、构造函数
为类中添加够高函数,可以直接传递DbContex。
public Repository() { } public Repository(DbContext dbContext) { DbContext = dbContext; }
3、查找实体方法Find
Find有一个重载。两个方法分别可以根据ID和根据lamdba表达式查找实体。
/// <summary> /// 查找实体 /// </summary> /// <param name="ID">实体主键值</param> /// <returns></returns> public T Find(int ID) { return DbContext.Set<T>().Find(ID); } /// <summary> /// 查找实体 /// </summary> /// <param name="where">查询Lambda表达式</param> /// <returns></returns> public T Find(Expression<Func<T,bool>> where) { return DbContext.Set<T>().SingleOrDefault(where); }
4、查找实体列表方法FindList
根据需要FindList进行多次重载
/// <summary> /// 查找实体列表 /// </summary> /// <returns></returns> public IQueryable<T> FindList() { return DbContext.Set<T>(); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="TKey">排序建类型</typeparam> /// <param name="order">排序表达式</param> /// <param name="asc">是否正序</param> /// <returns></returns> public IQueryable<T> FindList<TKey>(Expression<Func<T, TKey>> order, bool asc) { return asc ? DbContext.Set<T>().OrderBy(order) : DbContext.Set<T>().OrderByDescending(order); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="TKey">排序键类型</typeparam> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <param name="number">获取的记录数量</param> /// <returns></returns> public IQueryable<T> FindList<TKey>(Expression<Func<T, TKey>> order, bool asc,int number) { return asc ? DbContext.Set<T>().OrderBy(order).Take(number) : DbContext.Set<T>().OrderByDescending(order).Take(number); } /// <summary> /// 查找实体列表 /// </summary> /// <param name="where">查询Lambda表达式</param> /// <returns></returns> public IQueryable<T> FindList(Expression<Func<T, bool>> where) { return DbContext.Set<T>().Where(where); } /// <summary> /// 查找实体列表 /// </summary> /// <param name="where">查询Lambda表达式</param> /// <param name="number">获取的记录数量</param> /// <returns></returns> public IQueryable<T> FindList(Expression<Func<T, bool>> where, int number) { return DbContext.Set<T>().Where(where).Take(number); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="TKey">排序键类型</typeparam> /// <param name="where">查询Lambda表达式</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <returns></returns> public IQueryable<T> FindList<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, bool asc) { return asc ? DbContext.Set<T>().Where(where).OrderBy(order) : DbContext.Set<T>().Where(where).OrderByDescending(order); } /// <summary> /// 查找实体列表 /// </summary> /// <typeparam name="TKey">排序键类型</typeparam> /// <param name="where">查询Lambda表达式</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <param name="number">获取的记录数量</param> /// <returns></returns> public IQueryable<T> FindList<TKey>(Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, bool asc, int number) { return asc ? DbContext.Set<T>().Where(where).OrderBy(order).Take(number) : DbContext.Set<T>().Where(where).OrderByDescending(order).Take(number); }
5、查找实体分页列表方法FindPageList
根据需要FindPageList进行多次重载
/// <summary> /// 查找分页列表 /// </summary> /// <param name="pageSize">每页记录数。必须大于1</param> /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalNumber">总记录数</param> /// <returns></returns> public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>(); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); } /// <summary> /// 查找分页列表 /// </summary> /// <param name="pageSize">每页记录数。必须大于1</param> /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalNumber">总记录数</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> /// <returns></returns> public IQueryable<T> FindPageList<TKey>(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, TKey>> order, bool asc) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>(); _list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); } /// <summary> /// 查找分页列表 /// </summary> /// <param name="pageSize">每页记录数。必须大于1</param> /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalNumber">总记录数</param> /// <param name="where">查询表达式</param> public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>().Where(where); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); } /// <summary> /// 查找分页列表 /// </summary> /// <param name="pageSize">每页记录数。必须大于1</param> /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param> /// <param name="totalNumber">总记录数</param> /// <param name="where">查询表达式</param> /// <param name="order">排序键</param> /// <param name="asc">是否正序</param> public IQueryable<T> FindPageList<TKey>(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, bool asc) { if (pageIndex < 1) pageIndex = 1; if (pageSize < 1) pageSize = 10; IQueryable<T> _list = DbContext.Set<T>().Where(where); _list = asc ? _list.OrderBy(order) : _list.OrderByDescending(order); totalNumber = _list.Count(); return _list.Skip((pageIndex - 1) * pageIndex).Take(pageSize); }
6、添加实体方法Add
Add 方法有一个重载,重载方法可以选择是否立即保存
/// <summary> /// 添加实体 /// </summary> /// <param name="entity">实体</param> /// <returns>受影响的对象的数目</returns> public int Add(T entity) { return Add(entity, true); } /// <summary> /// 添加实体 /// </summary> /// <param name="entity">实体</param> /// <param name="isSave">是否立即保存</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Add(T entity,bool isSave) { DbContext.Set<T>().Add(entity); return isSave ? DbContext.SaveChanges() : 0; }
7、更新实体方法Update
Updae 方法有一个重载,重载方法可以选择是否立即保存
#region Update /// <summary> /// 更新实体【立即保存】 /// </summary> /// <param name="entity">实体</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Update(T entity) { return Update(entity, true); } /// <summary> /// 更新实体 /// </summary> /// <param name="entity">实体</param> /// <param name="isSave">是否立即保存</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Update(T entity, bool isSave) { DbContext.Set<T>().Attach(entity); DbContext.Entry<T>(entity).State = EntityState.Modified; return isSave ? DbContext.SaveChanges() : 0; }
8、删除实体方法Delete
Delete 方法有两个重载,一个是可以选择否立即保存,另一个是批量删除
/// <summary> /// 删除实体【立即保存】 /// </summary> /// <param name="entity">实体</param> /// <returns>受影响的对象的数目</returns> public int Delete(T entity) { return Delete(entity, true); } /// <summary> /// 删除实体 /// </summary> /// <param name="entity">实体</param> /// <param name="isSave">是否立即保存</param> /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns> public int Delete(T entity,bool isSave) { DbContext.Set<T>().Remove(entity); return isSave ? DbContext.SaveChanges() : 0; } /// <summary> /// 批量删除实体 /// </summary> /// <param name="entities">实体集合</param> /// <returns>受影响的对象的数目</returns> public int Delete(IEnumerable<T> entities) { DbContext.Set<T>().RemoveRange(entities); return DbContext.SaveChanges(); }
9、统计记录数方法Count
Count 方法有一个重载,可以根据表达式进行统计
/// <summary> /// 记录数 /// </summary> /// <returns></returns> public int Count() { return DbContext.Set<T>().Count(); } /// <summary> /// 记录数 /// </summary> /// <param name="predicate">表达式</param> /// <returns></returns> public int Count(Expression<Func<T, bool>> predicate) { return DbContext.Set<T>().Count(predicate); }
10、是否存在
/// <summary> /// 记录是否存在 /// </summary> /// <param name="predicate">表达式</param> /// <returns></returns> public bool IsContains(Expression<Func<T, bool>> predicate) { return Count(predicate) > 0; }
11、保存到数据库
/// <summary> /// 保存数据【在Add、Upate、Delete未立即保存的情况下使用】 /// </summary> /// <returns>受影响的记录数</returns> public int Save() { return DbContext.SaveChanges(); }