using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.Linq; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using Test.Model; namespace OrmTest { public class UnitNavUpdatee12 { public static void Init() { var db = NewUnitTest.Db; db.CodeFirst.InitTables(); db.CodeFirst.InitTables(); Demo1(db); Demo2(db); } private static void Demo1(SqlSugarClient db) { //清空初始化数据 db.DbMaintenance.TruncateTable(); db.DbMaintenance.TruncateTable(); //插入测试数据 db.InsertNav(new Student() { Age = "11", Name = "a", Books = new List() { new Book() { Name = "book" } }, Schools = new List() { new School(){ Name="学校", Playgrounds=new List() { new Playground(){ Name="学校GR"} } , Rooms=new List(){ new Room() { Name= "学校ROOM" } }, OtherThingOnes=new List() { new OtherThingOne(){ Name="学校One" } }, OtherThingTwos=new List() { new OtherThingTwo(){ Name="学校Two" } } } } }) .Include(s => s.Books) .Include(s => s.Schools).ThenInclude(sc => sc.Rooms) .Include(s => s.Schools).ThenInclude(sc => sc.Playgrounds) .Include(s => s.Schools).ThenInclude(sc => sc.OtherThingOnes) .Include(s => s.Schools).ThenInclude(sc => sc.OtherThingTwos) .ExecuteCommand(); var data = db.Queryable() .Includes(s => s.Books) .Includes(s => s.Schools, s => s.Rooms) .Includes(s => s.Schools, s => s.Playgrounds) .Includes(s => s.Schools, s => s.OtherThingOnes) .Includes(s => s.Schools, s => s.OtherThingTwos) .ToList(); data[0].Schools[0].OtherThingOnes[0].Name = "updateOne"; if (data.Count != 1 || data.First().Schools.Count != 1 || data.First().Schools.First().Rooms.Count() != 1 || data.First().Schools.First().Playgrounds.Count() != 1) { throw new Exception("unit error"); } //更新数据 db.UpdateNav(data) .Include(s => s.Schools).ThenInclude(sc => sc.Rooms) .Include(s => s.Schools).ThenInclude(sc => sc.Playgrounds) .Include(s => s.Schools).ThenInclude(sc => sc.OtherThingOnes) .Include(s => s.Schools).ThenInclude(sc => sc.OtherThingTwos) .Include(s => s.Books) .ExecuteCommand(); var data2 = db.Queryable() .Includes(s => s.Books) .Includes(s => s.Schools, s => s.Rooms) .Includes(s => s.Schools, s => s.Playgrounds) .Includes(s => s.Schools, s => s.OtherThingOnes) .Includes(s => s.Schools, s => s.OtherThingTwos) .ToList(); if (data2.Count != 1 || data2.First().Schools.Count != 1 || data2.First().Schools.First().Rooms.Count() != 1 || data2.First().Schools.First().Playgrounds.Count() != 1) { throw new Exception("unit error"); } } private static void Demo2(SqlSugarClient db) { db.DbMaintenance.TruncateTable(); db.InsertNav(new Student() { Age = "11", Name = "a", Books = new List() { new Book() { Name = "book" } }, Schools = new List() { new School(){ Name="学校", Playgrounds=new List() { new Playground(){ Name="学校GR"} } , Rooms=new List(){ new Room() { Name= "学校ROOM" } } } } }) // .Include(s => s.Schools) .Include(s => s.Schools).ThenInclude(sc => sc.Rooms) .Include(s => s.Schools).ThenInclude(sc => sc.Playgrounds) .Include(s => s.Books) .ExecuteCommand(); var data = db.Queryable() .Includes(s => s.Books) .Includes(s => s.Schools, s => s.Rooms) .Includes(s => s.Schools, s => s.Playgrounds) .ToList(); if (data.Count != 1 || data.First().Schools.Count != 1 || data.First().Schools.First().Rooms.Count() != 1 || data.First().Schools.First().Playgrounds.Count() != 1) { throw new Exception("unit error"); } foreach (var item in data) { item.Name = "st" + item.Name; foreach (var s in item.Schools) { s.Name = "shool" + s.Name; foreach (var si in s.Rooms) { si.Name += "1"; } foreach (var si in s.Playgrounds) { si.Name += "1"; } } } db.UpdateNav(data) .Include(s => s.Schools).ThenInclude(sc => sc.Rooms) .Include(s => s.Schools).ThenInclude(sc => sc.Playgrounds) .Include(s => s.Books) .ExecuteCommand(); var data2 = db.Queryable() //.Includes(s => s.Books) .Includes(s => s.Schools, s => s.Rooms) .Includes(s => s.Schools, s => s.Playgrounds) .ToList(); if (data2.Count != 1 || data2.First().Schools.Count != 1 || data2.First().Schools.First().Rooms.Count() != 1 || data2.First().Schools.First().Playgrounds.Count() != 1) { throw new Exception("unit error"); } } } }