.NET?ORM框架SqlSugar實現(xiàn)導(dǎo)航查詢功能
1、導(dǎo)航查詢特點(diǎn)
作用:主要處理主對象里面有子對象這種層級關(guān)系查詢
1.1 無外鍵開箱就用
其它ORM導(dǎo)航查詢 需要 各種配置或者外鍵,而SqlSugar則開箱就用,無外鍵,只需配置特性和主鍵就能使用
1.2 高性能優(yōu)
查詢 性能非常強(qiáng)悍
支持大數(shù)據(jù)分頁導(dǎo)航查詢
3.3 語法超級爽
注意:多級查詢時VS有時候沒提示直接寫就行了 ,相比 其他 .NET ORM語法要簡單的多
var list=db.Queryable<Test>()
.Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street)//多級查詢 有時候VS沒提示手寫
.Includes(x => x.ClassInfo)// 一級查詢
.ToList();
//多級查詢 加排序過濾
.Includes(x =>x.Provinces.Where(z=>z.Id>0).OrderBy(z=>z.Id).ToList(),x=>x.Citys,x=>x.Street)
// 一級查詢
.Includes(x =>x.ClassInfo)
.ToList();2、新導(dǎo)航查詢 ORM
適合有主鍵的常規(guī)操作, 請升級到5.0.6.8
2.1 一對一
//實體
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))]//一對一
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 == "北大")//可以對一級導(dǎo)航進(jìn)行過濾
.ToList();2.2 一對多
//實體
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))]//一對一
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 == "北大")//可以對一級導(dǎo)航進(jìn)行過濾
.ToList();2.3 多對多
//多對多
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:簡單用法
var list3= db.Queryable<A1>().Includes(x => x.BList).ToList();
//例2:支持子對象排序和過濾
var list3= db.Queryable<A1>().Includes(x => x.BList.Where(z=>z.Id>0).ToList()).ToList();
//例3:支持主表過濾 Any和Count
var list3= db.Queryable<A1>().Includes(x => x.BList)
.Where(x=>x.BList .Any(z=>z.Id ==1)).ToList();2.4 多級查詢
配置好實體類,我們可以多級查詢 ,.NET 中輕松多級查詢
var list=db.Queryable<Test>()
.Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street)//有時候沒提示 直接寫
.Includes(x => x.ClassInfo)// 一級查詢
.ToList();2.5 大數(shù)據(jù)分頁導(dǎo)航
適合一次性查詢1000條以上的導(dǎo)航
var list = new List<Tree1>();
db.Queryable<Tree1>()
.Includes(it => it.Child)
.ForEach(it => list.Add(it), 300); //每次查詢300條 更多用法:https://www.donet5.com/Home/Doc?typeId=2414
3、ORM無配置映射(高性能)
適合沒有主鍵或者復(fù)雜的一些操作
3.1 無配置映射實現(xiàn)二層
結(jié)構(gòu): Student->SchoolA
var list = db.Queryable<StudentA>().ToList();
db.ThenMapper(list, stu =>
{
//如果加Where不能帶有stu參數(shù),stu參數(shù)寫到 SetContext
stu.SchoolA=db.Queryable<SchoolA>().SetContext(scl=>scl.SchoolId,()=>stu.SchoolId,stu).FirstOrDefault();
});
// SetContext不會生成循環(huán)操作,高性能 和直接Where性能是不一樣的如果沒有SetContext那么這個查詢將會循環(huán)
3.2 無配置映射無限級
了解原理后我們用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();
});
//這兒只是用樹型結(jié)構(gòu)來證明可以實現(xiàn)無限級別導(dǎo)航查詢 ,實際開發(fā)中樹型結(jié)構(gòu)用ToTree實現(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án)操作,高性能 和直接Where性能是不一樣的4 、.NET ORM 未來計劃
Json to sql 正在開發(fā)中 ,未來將打造一套直接由前端操作數(shù)據(jù)庫的API
{
"Queryable":"order",
Select:[ [{SqlFunc_AggregateMin:["id"]},"id"], [{SqlFunc_GetDate:[]},"Date"] ]
}將支持 權(quán)限過濾 ,驗證,多表查詢,層級導(dǎo)航查詢 等
GitHUB 源碼:
https://github.com/donet5/SqlSugar
到此這篇關(guān)于.NET ORM框架SqlSugar實現(xiàn)導(dǎo)航查詢功能的文章就介紹到這了,更多相關(guān).NET ORM SqlSugar導(dǎo)航查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.NET 與樹莓派WS28XX 燈帶的顏色漸變動畫效果的實現(xiàn)
所謂顏色漸變動畫,首先,你要確定兩種顏色——起始色和最終色,比如從綠色變成紅色,綠色是起始,紅色是終點(diǎn)。這篇文章主要介紹了.NET 與樹莓派WS28XX 燈帶的顏色漸變動畫,需要的朋友可以參考下2021-12-12
使用VS2022在ASP.NET?Core中構(gòu)建輕量級服務(wù)
本文詳細(xì)講解了使用VS2022在ASP.NET?Core中構(gòu)建輕量級服務(wù)的方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
.NetCore?Web?Api?利用ActionFilterAttribute統(tǒng)一接口返回值格式及問題解析
在實際項目開發(fā)過程中,統(tǒng)一API返回值格式對前端或第三方調(diào)用將是非常必要的,在.NetCore中我們可以通過ActionFilterAttribute來進(jìn)行統(tǒng)一返回值的封裝,對.NetCore?Web?Api?統(tǒng)一接口返回值格式相關(guān)知識感興趣的朋友一起看看吧2022-03-03
ASP.NET中iframe框架點(diǎn)擊左邊頁面鏈接 右邊顯示鏈接頁面內(nèi)容
這篇文章主要介紹了ASP.NET中iframe框架點(diǎn)擊左邊頁面鏈接,右邊顯示鏈接頁面內(nèi)容的實現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-07-07
ASP.NET Core使用微軟官方類庫實現(xiàn)漢字轉(zhuǎn)拼音
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core使用微軟官方類庫實現(xiàn)漢字轉(zhuǎn)拼音,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
在asp.net中操作sql server數(shù)據(jù)庫的一些小技巧
在asp.net中操作sql server數(shù)據(jù)庫的一些小技巧...2006-09-09
.NET Core基于Generic Host實現(xiàn)后臺任務(wù)方法教程
這篇文章主要給大家介紹了關(guān)于.NET Core基于Generic Host實現(xiàn)后臺任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
ASP.NET ashx實現(xiàn)無刷新頁面生成驗證碼
這篇文章主要為大家詳細(xì)介紹了ASP.NET ashx實現(xiàn)無刷新頁面生成驗證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09

