最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

.NET?ORM框架SqlSugar實(shí)現(xiàn)導(dǎo)航查詢功能

 更新時(shí)間:2022年04月20日 14:57:44   作者:果糖大數(shù)據(jù)科技  
今天這篇文章分享一款好用簡單的ORM框架?SqlSugar,相比?EF?Core的導(dǎo)航查詢更加簡單,配置更加容易,幾分鐘就能上手,對(duì).NET?ORM框架SqlSugar實(shí)現(xiàn)導(dǎo)航查詢功能感興趣的朋友一起看看吧

1、導(dǎo)航查詢特點(diǎn)

作用:主要處理主對(duì)象里面有子對(duì)象這種層級(jí)關(guān)系查詢

1.1 無外鍵開箱就用

其它ORM導(dǎo)航查詢 需要 各種配置或者外鍵,而SqlSugar則開箱就用,無外鍵,只需配置特性和主鍵就能使用

1.2 高性能優(yōu) 

 查詢 性能非常強(qiáng)悍  

 支持大數(shù)據(jù)分頁導(dǎo)航查詢

3.3 語法超級(jí)爽

注意:多級(jí)查詢時(shí)VS有時(shí)候沒提示直接寫就行了 ,相比 其他 .NET ORM語法要簡單的多

var list=db.Queryable<Test>()
          .Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street)//多級(jí)查詢 有時(shí)候VS沒提示手寫
          .Includes(x => x.ClassInfo)// 一級(jí)查詢
          .ToList();
                 
       //多級(jí)查詢  加排序過濾
       .Includes(x =>x.Provinces.Where(z=>z.Id>0).OrderBy(z=>z.Id).ToList(),x=>x.Citys,x=>x.Street)
        // 一級(jí)查詢
       .Includes(x =>x.ClassInfo)
       .ToList();

2、新導(dǎo)航查詢 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)行過濾
           .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)行過濾
           .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:簡單用法
var list3= db.Queryable<A1>().Includes(x => x.BList).ToList(); 
 
 //例2:支持子對(duì)象排序和過濾
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 多級(jí)查詢

配置好實(shí)體類,我們可以多級(jí)查詢 ,.NET 中輕松多級(jí)查詢

var list=db.Queryable<Test>()
               .Includes(x => x.Provinces,x=>x.Citys ,x=>x.Street)//有時(shí)候沒提示 直接寫
               .Includes(x => x.ClassInfo)// 一級(jí)查詢
               .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 無配置映射實(shí)現(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ì)生成循環(huán)操作,高性能  和直接Where性能是不一樣的

如果沒有SetContext那么這個(gè)查詢將會(huì)循環(huán)

3.2  無配置映射無限級(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();
});
//這兒只是用樹型結(jié)構(gòu)來證明可以實(shí)現(xiàn)無限級(jí)別導(dǎo)航查詢 ,實(shí)際開發(fā)中樹型結(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 未來計(jì)劃

Json to sql  正在開發(fā)中 ,未來將打造一套直接由前端操作數(shù)據(jù)庫的API

{
"Queryable":"order",
 Select:[ [{SqlFunc_AggregateMin:["id"]},"id"], [{SqlFunc_GetDate:[]},"Date"] ]
}

將支持 權(quán)限過濾 ,驗(yàn)證,多表查詢,層級(jí)導(dǎo)航查詢 等  

GitHUB 源碼:

https://github.com/donet5/SqlSugar

到此這篇關(guān)于.NET ORM框架SqlSugar實(shí)現(xiàn)導(dǎo)航查詢功能的文章就介紹到這了,更多相關(guān).NET ORM SqlSugar導(dǎo)航查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

安龙县| 甘肃省| 茂名市| 大厂| 济南市| 万山特区| 伊吾县| 新乐市| 霸州市| 天柱县| 汝州市| 新乐市| 登封市| 馆陶县| 松阳县| 华蓥市| 富阳市| 南乐县| 南和县| 宁国市| 桂林市| 理塘县| 嘉黎县| 泗阳县| 缙云县| 勃利县| 昆山市| SHOW| 鄂托克旗| 鄄城县| 德惠市| 连南| 吴旗县| 丹巴县| 聂荣县| 马山县| 安福县| 铜陵市| 视频| 岳阳市| 连云港市|