HZY.Framework.Repository.EntityFramework
HZY.Framework EF Core 仓储:泛型仓储、软删除、审计字段、雪花 ID、分表、字段加密、事务、批量操作 ===== 使用教程 ===== 1、安装 Provider 包(SqlServer/MySql/PostgreSql/Oracle/Sqlite)并注册: builder.AddMySqlRepository<AppDbContext>(new RepositoryOptions { ConnectionString = "server=...;database=..." }); var app = builder.Build(); app.UseRepository(); 2、实体标记特性即生效: [TableId] → 主键自动雪花 ID(UuId/UuIdString 可选) [TableLogic] → 软删除:查询自动过滤,删除改写为 UPDATE [TableField(TableFieldFill.CreateTime)] → 审计自动填充 (CreateTime/CreateId/UpdateTime/UpdateId/ DeleteTime/DeleteId) 3、IRepository<T> 全部函数(同步/异步成对,Async 略)。 注入:public class MemberService(IRepository<Member> repo) ▎查询 repo.Select // 跟踪 IQueryable(链式 LINQ 起点) repo.SelectNoTracking // 免跟踪查询 repo.Find(w => w.Name == "hz") // 按表达式查单条 repo.FindById(1L) // 按主键查单条 repo.ToList(w => w.Age > 18) // 按表达式查列表 repo.ToListAll() // 全表 repo.GetAllListByCache(20) // 全表内存缓存(分钟) repo.Count() / repo.Any(w => ...) // 计数/存在 repo.QueryableBySql(sql, params) // 原生 SQL 转 IQueryable 继续链式 repo.QueryBySql(sql) / QueryDicBySql(sql) / QueryDataTableBySql(sql) repo.QuerySingleBySql<long>("SELECT COUNT(1) FROM member") repo.FillFieldsToDict(list) // 补全字典文本(配合 [Dict]) ▎插入(雪花 ID、审计字段自动填充) var member = repo.Insert(new Member { Name = "hz" }); repo.InsertRange(list); repo.InsertBatch(list); // 分块批量(按库参数上限拆分) ▎修改 repo.Update(member); // 整实体更新 repo.Update(member, "IgnoreField");// 忽略指定字段 repo.Update(old, updated); // 只更新差异字段 repo.UpdateRange(list); // 批量(另有 UpdateRangeById) repo.UpdateBulk(s => s.SetProperty(w => w.Age, 20), // update set where w => w.Name == "hz"); repo.UpdateBatch(list); // 分块批量 ▎插入或修改(按主键有无自动分流) repo.InsertOrUpdate(member); repo.InsertOrUpdateRange(list) / InsertOrUpdateBatch(list) ▎删除(含 [TableLogic] 时自动转软删除 UPDATE) repo.Delete(member); // 删除实体 repo.Delete(w => w.Age > 99); // 按表达式 repo.DeleteById(1L); // 按主键(另有 DeleteByIds) repo.DeleteBulk(w => w.Age > 99); // 直接 delete from where ▎原生 SQL 写入 repo.ExecuteSqlRaw("UPDATE member SET age={0} WHERE id={1}", 20, 1L); ▎批量原生 SQL(Provider 包启用,1 条 SQL 处理 N 行) repo.BulkInsertRange(list) / BulkUpdateRange(list) repo.BulkInsertOrUpdateRange(list) ▎IQueryable 扩展 repo.Select.WhereIf(cond, w => ...) // 条件成立才过滤 .ToPage(page, size) // 分页(另有 ToPageIf) ▎基础 repo.UnitOfWork // 工作单元(延迟提交) repo.GetContext() // 取 DbContext 4、字典映射(编码自动翻译为文本): [Dict("sys_dict", "code", "text", ToField = "GenderText")] public string? Gender { get; set; } // 存编码 M/F public string? GenderText { get; set; } // 查询后自动填 男/女 5、事务(AOP,支持嵌套、多库): [Transactional] public async Task CreateOrderAsync(Order order) { } [Transactional(typeof(AppDbContext), typeof(ErpDbContext))] public async Task SyncErpAsync(Order order) { } 6、SQL 监控:optionsBuilder.AddEntityFrameworkMonitor(isMonitor: true) 源码与完整文档:https://gitee.com/hzy6/HZY.Framework 实战案例(HzyAdmin):https://gitee.com/hzy6/HzyAdmin
Activity
- Latest release
- 3w ago
- Total releases
- 152
- Cadence
- ~daily
- Last 12 months
- 28
Details
- License
- MIT
- First release
- Nov 24, 2023
| Version | Released | |
|---|---|---|
10.1.18
patch
|
10.1.18
patch
Dependencies (8)
|
|
10.1.17
patch
|
10.1.17
patch
Dependencies (8)
|
|
10.1.16
patch
|
10.1.16
patch
Dependencies (8)
|
|
10.1.15
patch
|
10.1.15
patch
Dependencies (8)
|
|
10.1.14
patch
|
10.1.14
patch
Dependencies (8)
|
|
10.1.10
patch
|
10.1.10
patch
Dependencies (8)
|
|
10.1.9
patch
|
10.1.9
patch
Dependencies (8)
|
|
10.1.8
patch
|
10.1.8
patch
Dependencies (8)
|
|
10.1.7
patch
|
10.1.7
patch
Dependencies (8)
|
|
10.1.6
patch
|
10.1.6
patch
Dependencies (8)
|
|
10.1.5
patch
|
10.1.5
patch
Dependencies (8)
|
|
10.1.4
patch
|
10.1.4
patch
Dependencies (8)
|
|
10.1.3
patch
|
10.1.3
patch
Dependencies (8)
|
|
10.1.2
patch
|
10.1.2
patch
Dependencies (8)
|
|
10.1.1
minor
|
10.1.1
minor
Dependencies (8)
|
|
10.0.13
patch
|
10.0.13
patch
Dependencies (21)
+ 13 more |
|
10.0.12
patch
|
10.0.12
patch
Dependencies (21)
+ 13 more |
|
10.0.11
patch
|
10.0.11
patch
Dependencies (21)
+ 13 more |
|
10.0.9
patch
|
10.0.9
patch
Dependencies (21)
+ 13 more |
|
10.0.8
patch
|
10.0.8
patch
Dependencies (21)
+ 13 more |
|
10.0.7
patch
|
10.0.7
patch
Dependencies (21)
+ 13 more |
|
10.0.6
patch
|
10.0.6
patch
Dependencies (21)
+ 13 more |
|
10.0.5
patch
|
10.0.5
patch
Dependencies (21)
+ 13 more |
|
10.0.4
patch
|
10.0.4
patch
Dependencies (21)
+ 13 more |
|
10.0.3
patch
|
10.0.3
patch
Dependencies (21)
+ 13 more |
|
10.0.2
patch
|
10.0.2
patch
Dependencies (21)
+ 13 more |
|
10.0.1
patch
|
10.0.1
patch
Dependencies (21)
+ 13 more |
|
10.0.0
major
|
10.0.0
major
Dependencies (21)
+ 13 more |
|
8.0.66
patch
|
8.0.66
patch
Dependencies (21)
+ 13 more |
|
8.0.65
patch
|
8.0.65
patch
Dependencies (20)
+ 12 more |
|
8.0.64
patch
|
8.0.64
patch
Dependencies (20)
+ 12 more |
|
8.0.63
patch
|
8.0.63
patch
Dependencies (20)
+ 12 more |
|
8.0.62
patch
|
8.0.62
patch
Dependencies (19)
+ 11 more |
|
8.0.61
patch
|
8.0.61
patch
Dependencies (19)
+ 11 more |
|
8.0.60
patch
|
8.0.60
patch
Dependencies (19)
+ 11 more |
|
8.0.59
patch
|
8.0.59
patch
Dependencies (19)
+ 11 more |
|
8.0.58
patch
|
8.0.58
patch
Dependencies (19)
+ 11 more |
|
8.0.57
patch
|
8.0.57
patch
Dependencies (19)
+ 11 more |
|
8.0.56
patch
|
8.0.56
patch
Dependencies (18)
+ 10 more |
|
8.0.55
patch
|
8.0.55
patch
Dependencies (18)
+ 10 more |
|
8.0.54
patch
|
8.0.54
patch
Dependencies (18)
+ 10 more |
|
8.0.53
patch
|
8.0.53
patch
Dependencies (18)
+ 10 more |
|
8.0.52
patch
|
8.0.52
patch
Dependencies (18)
+ 10 more |
|
8.0.51
patch
|
8.0.51
patch
Dependencies (18)
+ 10 more |
|
8.0.50
patch
|
8.0.50
patch
Dependencies (18)
+ 10 more |
|
8.0.49
patch
|
8.0.49
patch
Dependencies (18)
+ 10 more |
|
8.0.48
patch
|
8.0.48
patch
Dependencies (18)
+ 10 more |
|
8.0.47
patch
|
8.0.47
patch
Dependencies (18)
+ 10 more |
|
8.0.46
patch
|
8.0.46
patch
Dependencies (18)
+ 10 more |
|
8.0.45
patch
|
8.0.45
patch
Dependencies (18)
+ 10 more |