Dapper 鏈式查詢擴展 DapperSqlMaker Github地址:https://github.com/mumumutou/DapperSqlMaker 歡迎大佬加入 Demo: 查詢 TestsDapperSqlMaker\DapperSqlMaker.Test\ SelectDappe ...
Dapper 鏈式查詢擴展 DapperSqlMaker
Github地址:https://github.com/mumumutou/DapperSqlMaker 歡迎大佬加入
Demo:
- 查詢 TestsDapperSqlMaker\DapperSqlMaker.Test\ SelectDapperSqlMakerTest.cs
- 添加 TestsDapperSqlMaker\DapperSqlMaker.Test\ InsertDapperSqlMakerTest.cs
- 更新 TestsDapperSqlMaker\DapperSqlMaker.Test\ UpdateDapperSqlMakerTest.cs
- 刪除 TestsDapperSqlMaker\DapperSqlMaker.Test\ DeleteDapperSqlMakerTest.cs
- 上下文類 TestsDapperSqlMaker\DbDapperSqlMaker\ LockDapperUtilsqlite.cs
簡單慄子:
1.查詢-聯表查詢,分頁
1 public void 三表聯表分頁測試() 2 { 3 var arruser = new int[2] { 1,2 }; // 4 string uall = "b.*", pn1 = "%蛋蛋%", pn2 = "%m%"; 5 LockPers lpmodel = new LockPers() { IsDel = false}; 6 Users umodel = new Users() { UserName = "jiaojiao" }; 7 SynNote snmodel = new SynNote() { Name = "木頭" }; 8 Expression<Func<LockPers, Users, SynNote, bool>> where = PredicateBuilder.WhereStart<LockPers, Users, SynNote>(); 9 where = where.And((l, u, s) => ( l.Name.Contains(pn1) || l.Name.Contains(pn2) )); 10 where = where.And((lpw, uw, sn) => lpw.IsDel == lpmodel.IsDel); 11 where = where.And((l, u, s) => u.UserName == umodel.UserName); 12 where = where.And((l, u, s) => s.Name == snmodel.Name ); 13 where = where.And((l, u, s) => SM.In(u.Id, arruser)); 14 15 DapperSqlMaker<LockPers, Users, SynNote> query = LockDapperUtilsqlite<LockPers, Users, SynNote> 16 .Selec() 17 .Column((lp, u, s) => //null) //查詢所有欄位 18 new { lp.Name, lpid = lp.Id, x = "LENGTH(a.Prompt) as len", b = SM.Sql(uall)
, scontent = s.Content, sname = s.Name }) 19 .FromJoin(JoinType.Left, (lpp, uu, snn) => uu.Id == lpp.UserId 20 , JoinType.Inner, (lpp, uu, snn) => uu.Id == snn.UserId) 21 .Where(where) 22 .Order((lp, w, sn) => new { lp.EditCount, x = SM.OrderDesc(lp.Name), sn.Content }); 23 24 var result = query.ExcuteSelect(); 25 WriteJson(result); // 查詢結果 26 27 Tuple<StringBuilder, DynamicParameters> resultsqlparams = query.RawSqlParams(); 28 WriteSqlParams(resultsqlparams); // 列印sql和參數 29 30 int page = 2, rows = 3, records; 31 var result2 = query.LoadPagelt(page, rows, out records); 32 WriteJson(result2); // 查詢結果 33 }
生成的sql :
select a.Name as Name, a.Id as lpid , LENGTH(a.Prompt) as len, b.* , c.Content as scontent, c.Name as sname from LockPers a left join Users b on b.Id = a.UserId inner join SynNote c on b.Id = c.UserId where ( a.Name like @Name0 or a.Name like @Name1 ) and a.IsDel = @IsDel2 and b.UserName = @UserName3 and c.Name = @Name4 and b.Id in @Id order by a.EditCount, a.Name desc , c.Content
2.更新-更新部分欄位
1 [Test] 2 public void 更新部分欄位測試lt() 3 { 4 var issucs = LockDapperUtilsqlite<LockPers>.Cud.Update( 5 s => 6 { 7 s.Name = "測試bool修改1"; 8 s.Content = "update方法內賦值修改欄位"; 9 s.IsDel = true; 10 }, 11 w => w.Name == "測試bool修改1" && w.IsDel == true 12 ); 13 Console.WriteLine(issucs); 14 }
3.七聯表以上待擴展 copy六聯表修改3個文件
- DapperSqlMaker
- Template_DapperSqlMaker 上下文類
- PredicateBuilder 條件拼接類
4.實體生成T4模板使用方法 https://www.cnblogs.com/cl-blogs/p/7205954.html