From 1566d043891cf6ba3b50950b9112e1b346865f6b Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 26 Dec 2022 11:24:21 +0800 Subject: [PATCH] Update demo --- .../SqlSeverTest/Demo/Demo3_Insertable.cs | 268 +----------------- 1 file changed, 5 insertions(+), 263 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSeverTest/Demo/Demo3_Insertable.cs b/Src/Asp.NetCore2/SqlSeverTest/Demo/Demo3_Insertable.cs index 664aff811..58d613ef6 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/Demo/Demo3_Insertable.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/Demo/Demo3_Insertable.cs @@ -64,273 +64,15 @@ namespace OrmTest db.DbMaintenance.TruncateTable("TwoItem2"); db.DbMaintenance.TruncateTable("TwoItem3"); db.DbMaintenance.TruncateTable("ThreeItem2"); - Console.WriteLine("SubInsert Start"); - db.Insertable(new Order() - { - Name = "订单 1", - CustomId = 1, - Price = 100, - CreateTime = DateTime.Now, - Id = 0, - Items = new List() { - new OrderItem(){ - CreateTime=DateTime.Now, - OrderId=0, - Price=1, - ItemId=1 - }, - new OrderItem(){ - CreateTime=DateTime.Now, - OrderId=0, - Price=2, - ItemId=2 - } - } - }) - .AddSubList(it => it.Items.First().OrderId).ExecuteCommand(); - - - - db.Insertable(new List() { - new RootTable0() - { - Name="aa", - TwoItem2=new TwoItem2() { - Id="1", - ThreeItem2=new List(){ - new ThreeItem2(){ Name="a", TwoItem2Id="1" }, - new ThreeItem2(){ Id=2, Name="a2", TwoItem2Id="2" } - } - }, - TwoItem=new TwoItem() - { - Name ="itema" , - RootId=2 - }, - TwoItem3=new List(){ - new TwoItem3(){ Id=0, Name="a",Desc="" }, - - } - }, - new RootTable0() - { - Name="bb", - TwoItem2=new TwoItem2() { - Id="2" - }, - TwoItem=new TwoItem() - { - Name ="itemb" , - RootId=2, - - }, - TwoItem3=new List(){ - new TwoItem3(){ Id=1, Name="b",Desc="" }, - new TwoItem3(){ Id=2, Name="b1",Desc="1" }, - } - } - }) - .AddSubList(it => it.TwoItem.RootId) - .AddSubList(it => new SubInsertTree() - { - Expression = it.TwoItem2.RootId, - ChildExpression = new List() { - new SubInsertTree(){ - Expression=it.TwoItem2.ThreeItem2.First().TwoItem2Id - } - } - }) - .AddSubList(it => it.TwoItem3) - .ExecuteCommand(); + object getAll = db.Queryable().Take(2).ToList(); + db.InsertableByObject(getAll).ExecuteCommand(); + object o = db.Queryable().First(); + db.InsertableByObject(o).ExecuteCommand(); - SubNoIdentity(db); - SubIdentity(db); Console.WriteLine("#### Insertable End ####"); } - - private static void SubNoIdentity(SqlSugarClient db) - { - db.CodeFirst.InitTables(); - db.DbMaintenance.TruncateTable("Country"); - db.DbMaintenance.TruncateTable("Province"); - db.DbMaintenance.TruncateTable("City"); - db.Insertable(new List() - { - new Country(){ - Id=1, - Name="中国", - Provinces=new List(){ - new Province{ - Id=1001, - Name="江苏", - citys=new List(){ - new City(){ Id=1001001, Name="南通" }, - new City(){ Id=1001002, Name="南京" } - } - }, - new Province{ - Id=1002, - Name="上海", - citys=new List(){ - new City(){ Id=1002001, Name="徐汇" }, - new City(){ Id=1002002, Name="普陀" } - } - }, - new Province{ - Id=1003, - Name="北京", - citys=new List(){ - new City(){ Id=1003001, Name="北京A" }, - new City(){ Id=1003002, Name="北京B" } - } - } - } - }, - new Country(){ - Name="美国", - Id=2, - Provinces=new List() - { - new Province(){ - Name="美国小A", - Id=20001 - }, - new Province(){ - Name="美国小b", - Id=20002 - } - } - }, - new Country(){ - Name="英国", - Id=3 - } - }) - .AddSubList(it => new SubInsertTree() - { - Expression = it.Provinces.First().CountryId, - ChildExpression = new List() { - new SubInsertTree(){ - Expression=it.Provinces.First().citys.First().ProvinceId - } - } - }) - .ExecuteCommand(); - - var list = db.Queryable() - .Mapper(it => it.Provinces, it => it.Provinces.First().CountryId) - .Mapper(it => - { - foreach (var item in it.Provinces) - { - item.citys = db.Queryable().Where(y => y.ProvinceId == item.Id).ToList(); - } - }) - .ToList(); - } - private static void SubIdentity(SqlSugarClient db) - { - db.CodeFirst.InitTables(); - db.DbMaintenance.TruncateTable("Country1"); - db.DbMaintenance.TruncateTable("Province1"); - db.DbMaintenance.TruncateTable("City1"); - db.DbMaintenance.TruncateTable("Area1"); - var list = new List() - { - new Country1(){ - Id=1, - Name="中国", - Provinces=new List(){ - new Province1{ - Id=1001, - Name="江苏", - citys=new List(){ - new City1(){ - ProvinceId=1001, - Name ="昆山", - area=new List() { - new Area1(){ - CityId=000, - Name="江苏小县城" - - } - } - - } - }, - - }, - new Province1{ - Id=1002, - Name="上海", - citys=new List(){ - new City1(){ - ProvinceId=1002, - Name ="陆家嘴" - } - - - } - }, - new Province1{ - Id=1003, - Name="北京", - citys=new List(){ - new City1(){ - ProvinceId=1003 , - Name ="中官村" - } - - - } - } - } - }, - new Country1(){ - Name="美国", - Id=2, - Provinces=new List() - { - new Province1(){ - Name="美国小A", - Id=20001 - }, - new Province1(){ - Name="美国小b", - Id=20002 - } - } - }, - new Country1(){ - Name="英国", - Id=3 - } - }; - //开始插入 - db.Insertable(list) - .AddSubList(it => new SubInsertTree() - { - Expression = it.Provinces.First().CountryId,//CountryId自动填充 - ChildExpression = new List() { - new SubInsertTree(){ - Expression=it.Provinces.First() - .citys.First() - .ProvinceId,//ProvinceId自动填充, - ChildExpression=new List(){ - new SubInsertTree(){ - Expression = it.Provinces.First().citys.First().area.First().CityId - } - - } - } - } - }) - //如果有多个子结果这里还能在.AddSubList - .ExecuteCommand(); - - var list2=db.Queryable().Includes(x => x.Provinces, x => x.citys, x => x.area).ToList(); - } + } }