ToList:获取结果集
var list = await _dbSet.Find(m => m.Id > 100 && m.Id < 120).ToList(); 3、特性 表别名以及列名 [Table("blog_article")] public class Article : EntityBase { [Column("Title")] public string Title1 { get; set; } public string Summary { get; set; } public string Body { get; set; } public Category Category { get; set; } public int ReadCount { get; set; } public bool IsDeleted { get; set; } public DateTime CreatedTime { get; set; } } 指定主键可以通过KeyAttribute来指定某个字段为主键
4、泛型仓储(Repository)平时开发时用到伪 DDD 比较多,所以框架提供了一个泛型仓储接口IRepository以及一个抽象实现RepositoryAbstract
/// <summary> /// 判断是否存在 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> where, IDbTransaction transaction = null); /// <summary> /// 新增 /// </summary> /// <param>实体</param> /// <param>事务</param> /// <returns></returns> Task<bool> AddAsync(TEntity entity, IDbTransaction transaction = null); /// <summary> /// 批量新增 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<bool> AddAsync(List<TEntity> list, IDbTransaction transaction = null); /// <summary> /// 删除 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<bool> DeleteAsync(dynamic id, IDbTransaction transaction = null); /// <summary> /// 更新 /// </summary> /// <param>实体</param> /// <param>事务</param> /// <returns></returns> Task<bool> UpdateAsync(TEntity entity, IDbTransaction transaction = null); /// <summary> /// 根据主键查询 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<TEntity> GetAsync(dynamic id, IDbTransaction transaction = null); /// <summary> /// 根据表达式查询单条记录 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<TEntity> GetAsync(Expression<Func<TEntity,bool>> where, IDbTransaction transaction = null); /// <summary> /// 分页查询 /// </summary> /// <param>分页</param> /// <param>过滤条件</param> /// <param>事务</param> /// <returns></returns> Task<List<TEntity>> PaginationAsync(Paging paging = null, Expression<Func<TEntity, bool>> where = null, IDbTransaction transaction = null);RepositoryAbstract中包含实体对应的数据集IDbSet以及数据上限为IDbContext
protected readonly IDbSet<TEntity> Db; protected readonly IDbContext DbContext; protected RepositoryAbstract(IDbContext dbContext) { DbContext = dbContext; Db = dbContext.Set<TEntity>(); }对于事务,建议使用工作单元IUnitOfWork
public interface IUnitOfWork { /// <summary> /// 打开一个事务 /// </summary> /// <returns></returns> IDbTransaction BeginTransaction(); /// <summary> /// 提交 /// </summary> /// <returns></returns> void Commit(); /// <summary> /// 回滚 /// </summary> void Rollback(); }项目已经包含了一个实现UnitOfWork
6、仓储使用方法 6.1、定义仓储 public interface IArticleRepository : IRepository<Article> { } 6.2、创建仓储实例 private readonly IArticleRepository _repository; public RepositoryTest() { var dbContext = new BlogDbContext(new SQLiteDbContextOptions("Filename=./Database/Test.db")); _repository = new ArticleRepository(dbContext); } 6.3、新增 [Fact] public async void AddTest() { var article = new Article { Title1 = "test", Category = Category.Blog, Summary = "这是一篇测试文章", Body = "这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章", ReadCount = 10, IsDeleted = true, CreatedTime = DateTime.Now }; await _repository.AddAsync(article); Assert.True(article.Id > 0); } 6.4、批量增加 [Fact] public void BatchInsertTest() { var list = new List<Article>(); for (var i = 0; i < 10000; i++) { var article = new Article { Title1 = "test" + i, Category = i % 3 == 1 ? Category.Blog : Category.Movie, Summary = "这是一篇测试文章", Body = "这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章这是一篇测试文章", ReadCount = 10, IsDeleted = i % 2 == 0, CreatedTime = DateTime.Now }; list.Add(article); } var sw = new Stopwatch(); sw.Start(); _repository.AddAsync(list); sw.Stop(); var s = sw.ElapsedMilliseconds; Assert.True(s > 0); } 6.5、删除 [Fact] public async void DeleteTest() { var b = await _repository.DeleteAsync(2); Assert.True(b); } 6.6、修改 [Fact] public async void UpdateTest() { var article = await _repository.GetAsync(2); article.Title1 = "修改测试"; var b = await _repository.UpdateAsync(article); Assert.True(b); } 6.7、分页查询 [Fact] public async void PaginationTest() { var paging = new Paging(1, 20); var list = await _repository.PaginationAsync(paging, m => m.Id > 1000); Assert.True(paging.TotalCount > 0); } 未完待续~