.NET?ORM框架SqlSugar實(shí)現(xiàn)導(dǎo)航查詢(xún)功能
1、導(dǎo)航查詢(xún)特點(diǎn)
作用:主要處理主對(duì)象里面有子對(duì)象這種層級(jí)關(guān)系查詢(xún)
1.1 無(wú)外鍵開(kāi)箱就用
其它ORM導(dǎo)航查詢(xún) 需要 各種配置或者外鍵,而SqlSugar則開(kāi)箱就用,無(wú)外鍵,只需配置特性和主鍵就能使用
1.2 高性能優(yōu)
查詢(xún) 性能非常強(qiáng)悍
支持大數(shù)據(jù)分頁(yè)導(dǎo)航查詢(xún)
3.3 語(yǔ)法超級(jí)爽
注意:多級(jí)查詢(xún)時(shí)VS有時(shí)候沒(méi)提示直接寫(xiě)就行了 ,相比 其他 .NET ORM語(yǔ)法要簡(jiǎn)單的多
var list=db.Queryable<Test>() .Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street)//多級(jí)查詢(xún) 有時(shí)候VS沒(méi)提示手寫(xiě) .Includes(x => x.ClassInfo)// 一級(jí)查詢(xún) .ToList(); //多級(jí)查詢(xún) 加排序過(guò)濾 .Includes(x =>x.Provinces.Where(z=>z.Id>0).OrderBy(z=>z.Id).ToList(),x=>x.Citys,x=>x.Street) // 一級(jí)查詢(xún) .Includes(x =>x.ClassInfo) .ToList();
2、新導(dǎo)航查詢(xún) ORM
適合有主鍵的常規(guī)操作, 請(qǐng)升級(jí)到5.0.6.8
2.1 一對(duì)一
//實(shí)體 public class StudentA { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int StudentId { get; set; } public string Name { get; set; } public int SchoolId { get; set; } [Navigate(NavigateType.OneToOne, nameof(SchoolId))]//一對(duì)一 public SchoolA SchoolA { get; set; } } public class SchoolA { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int SchoolId { get; set; } public string SchoolName { get; set; } } //代碼 var list2 = db.Queryable<StudentA>() .Includes(x => x.SchoolA) .Where(x => x.SchoolA.SchoolName == "北大")//可以對(duì)一級(jí)導(dǎo)航進(jìn)行過(guò)濾 .ToList();
2.2 一對(duì)多
//實(shí)體 public class StudentA { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int StudentId { get; set; } public string Name { get; set; } public int SchoolId { get; set; } [Navigate(NavigateType.OneToOne, nameof(SchoolId))]//一對(duì)一 public SchoolA SchoolA { get; set; } } public class SchoolA public string SchoolName { get; set; } //代碼 var list2 = db.Queryable<StudentA>() .Includes(x => x.SchoolA) .Where(x => x.SchoolA.SchoolName == "北大")//可以對(duì)一級(jí)導(dǎo)航進(jìn)行過(guò)濾 .ToList();
2.3 多對(duì)多
//多對(duì)多 public class ABMapping1 { [SugarColumn(IsPrimaryKey = true )] public int AId { get; set; } [SugarColumn(IsPrimaryKey = true)] public int BId { get; set; } } public class A1 { [SugarColumn(IsPrimaryKey = true, IsIdentity = true )] public int Id { get; set; } public string Name { get; set; } [Navigate(typeof(ABMapping1),nameof(ABMapping1.AId),nameof(ABMapping1.BId))] public List<B1> BList { get; set; } } public class B1 { [SugarColumn(IsPrimaryKey = true , IsIdentity = true)] public int Id { get; set; } public string Name { get; set; } [Navigate(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))] public List<A1> AList { get; set; } } //例1:簡(jiǎn)單用法 var list3= db.Queryable<A1>().Includes(x => x.BList).ToList(); //例2:支持子對(duì)象排序和過(guò)濾 var list3= db.Queryable<A1>().Includes(x => x.BList.Where(z=>z.Id>0).ToList()).ToList(); //例3:支持主表過(guò)濾 Any和Count var list3= db.Queryable<A1>().Includes(x => x.BList) .Where(x=>x.BList .Any(z=>z.Id ==1)).ToList();
2.4 多級(jí)查詢(xún)
配置好實(shí)體類(lèi),我們可以多級(jí)查詢(xún) ,.NET 中輕松多級(jí)查詢(xún)
var list=db.Queryable<Test>() .Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street)//有時(shí)候沒(méi)提示 直接寫(xiě) .Includes(x => x.ClassInfo)// 一級(jí)查詢(xún) .ToList();
2.5 大數(shù)據(jù)分頁(yè)導(dǎo)航
適合一次性查詢(xún)1000條以上的導(dǎo)航
var list = new List<Tree1>(); db.Queryable<Tree1>() .Includes(it => it.Child) .ForEach(it => list.Add(it), 300); //每次查詢(xún)300條
更多用法:https://www.donet5.com/Home/Doc?typeId=2414
3、ORM無(wú)配置映射(高性能)
適合沒(méi)有主鍵或者復(fù)雜的一些操作
3.1 無(wú)配置映射實(shí)現(xiàn)二層
結(jié)構(gòu): Student->SchoolA
var list = db.Queryable<StudentA>().ToList(); db.ThenMapper(list, stu => { //如果加Where不能帶有stu參數(shù),stu參數(shù)寫(xiě)到 SetContext stu.SchoolA=db.Queryable<SchoolA>().SetContext(scl=>scl.SchoolId,()=>stu.SchoolId,stu).FirstOrDefault(); }); // SetContext不會(huì)生成循環(huán)操作,高性能 和直接Where性能是不一樣的
如果沒(méi)有SetContext那么這個(gè)查詢(xún)將會(huì)循環(huán)
3.2 無(wú)配置映射無(wú)限級(jí)
了解原理后我們用ThenMapper想映射哪層就映射哪層
var treeRoot=db.Queryable<Tree>().Where(it => it.Id == 1).ToList(); //第一層 db.ThenMapper(treeRoot, item => { item.Child = db.Queryable<Tree>().SetContext(x => x.ParentId, () => item.Id, item).ToList(); }); //第二層 db.ThenMapper(treeRoot.SelectMany(it=>it.Child), it => { it.Child = db.Queryable<Tree>().SetContext(x => x.ParentId, () => it.Id, it).ToList(); }); //第三層 db.ThenMapper(treeRoot.SelectMany(it => it.Child).SelectMany(it=>it.Child), it => { it.Child = db.Queryable<Tree>().SetContext(x => x.ParentId, () => it.Id, it).ToList(); }); //這兒只是用樹(shù)型結(jié)構(gòu)來(lái)證明可以實(shí)現(xiàn)無(wú)限級(jí)別導(dǎo)航查詢(xún) ,實(shí)際開(kāi)發(fā)中樹(shù)型結(jié)構(gòu)用ToTree實(shí)現(xiàn) public class Tree { [SqlSugar.SugarColumn(IsPrimaryKey =true)] public int Id { get; set; } public string Name { get; set; } public int ParentId { get; set; } [SqlSugar.SugarColumn(IsIgnore = true)] public Tree Parent { get; set; } [SqlSugar.SugarColumn(IsIgnore = true)] public List<Tree> Child { get; set; } } // SetContext不會(huì)生成循環(huán)操作,高性能 和直接Where性能是不一樣的
4 、.NET ORM 未來(lái)計(jì)劃
Json to sql 正在開(kāi)發(fā)中 ,未來(lái)將打造一套直接由前端操作數(shù)據(jù)庫(kù)的API
{ "Queryable":"order", Select:[ [{SqlFunc_AggregateMin:["id"]},"id"], [{SqlFunc_GetDate:[]},"Date"] ] }
將支持 權(quán)限過(guò)濾 ,驗(yàn)證,多表查詢(xún),層級(jí)導(dǎo)航查詢(xún) 等
GitHUB 源碼:
https://github.com/donet5/SqlSugar
到此這篇關(guān)于.NET ORM框架SqlSugar實(shí)現(xiàn)導(dǎo)航查詢(xún)功能的文章就介紹到這了,更多相關(guān).NET ORM SqlSugar導(dǎo)航查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在.net core中實(shí)現(xiàn)字段和屬性注入的示例代碼
這篇文章主要介紹了在.net core中實(shí)現(xiàn)字段和屬性注入的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08asp.net Request.ServerVariables[] 讀解
asp.net Request.ServerVariables[] 讀解,學(xué)習(xí).net的朋友可以參考下,方便獲取服務(wù)器的一些信息。2011-08-08.NET?core項(xiàng)目AsyncLocal在鏈路追蹤中的應(yīng)用
這篇文章主要為大家介紹了.NET?core項(xiàng)目zhong?AsyncLocal在鏈路追蹤中的應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05ASP.NET 網(wǎng)站開(kāi)發(fā)中常用到的廣告效果代碼
在A(yíng)SP.NET項(xiàng)目開(kāi)發(fā)中,會(huì)被要求添加廣告,有翻屏效果、有廣告輪流顯示、飄浮廣告、左側(cè)廣告、右側(cè)廣告等。2010-04-04Asp.NET Core 如何調(diào)用WebService的方法
這篇文章主要介紹了Asp.NET Core 如何調(diào)用WebService的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08CSRF在A(yíng)SP.NET Core中的處理方法詳解
這篇文章主要給大家介紹了關(guān)于CSRF在A(yíng)SP.NET Core中的處理方法,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07asp.net中頁(yè)面顯示當(dāng)前系統(tǒng)時(shí)間附圖
asp.net如何實(shí)現(xiàn)在頁(yè)面顯示當(dāng)前系統(tǒng)時(shí)間,本文有個(gè)不錯(cuò)的方法,大家可以嘗試操作下,在文章末尾有截圖2013-12-12