diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index e7d96f390..0b2b37981 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -97,6 +97,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 26b731f79..24197d562 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UnitInsertNav2.Init(); UnitInsertNav.Init(); UnitInsertNav.Init(); UnitSelectN.Init(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav2.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav2.cs new file mode 100644 index 000000000..00ee7c3f6 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav2.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; + +using SqlSugar; + +namespace OrmTest +{ + public class UnitInsertNav2 + { + public static void Init() + { + var db = NewUnitTest.Db; + //建表 + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + //插入一级导航数据 + var country = new Country + { + Id = 1, + Provinces = new List { + new Province{ + Id = 1, + CountryId = 1 + }, + } + }; + db.InsertNav(country) + .Include(it => it.Provinces) + .ExecuteCommand(); + + //构造二级导航数据 + country.Provinces[0].Cities = new List + { + new City + { + Id = 1, + ProvinceId = 1 + } + }; + + //用例代码 + var result = db.InsertNav(country) + .Include(it => it.Provinces) + .ThenInclude(it => it.Cities) + .ExecuteCommand();//用例代码 + if (!db.Queryable().Where(it => it.Id == 1).Any()) + throw new Exception("未插入二级导航数据"); + } + + + //建类 + [SugarTable("Unitaaa1Country")] + public class Country + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + [SugarColumn(IsIgnore = true)] + [Navigate(NavigateType.OneToMany, nameof(Province.CountryId))] + public List Provinces { get; set; } + } + [SugarTable("Unitaaa1Province")] + public class Province + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + public long CountryId { get; set; } + [SugarColumn(IsIgnore = true)] + [Navigate(NavigateType.OneToMany, nameof(City.ProvinceId))] + public List Cities { get; set; } + } + [SugarTable("Unitaaa1City")] + public class City + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + public long ProvinceId { get; set; } + } + } +} \ No newline at end of file