From 929a0605a46347872ebbbe4b2625f9056ec7313a Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 11 Sep 2023 01:19:10 +0800 Subject: [PATCH] Add unit test --- .../SqlSeverTest/UnitTest/Main.cs | 1 + .../SqlSeverTest/UnitTest/UnitOneToMany.cs | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToMany.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs index 01dc7b1f6..9962f3e1a 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UnitOneToMany.Init(); UnitOneToOneDel.Init(); EntityInfoTest.Init(); UnitOneToManyafdaa.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToMany.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToMany.cs new file mode 100644 index 000000000..e5bba7619 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToMany.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OrmTest +{ + internal class UnitOneToMany + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + + var address = new UnitAddress011 + { + Street = "123 Main Street", + CityId=1 + }; + int addressId = db.Insertable(address).ExecuteReturnIdentity(); + + // 创建 UnitPerson011 对象并插入记录 + var person = new UnitPerson011 + { + Name = "John Doe", + AddressId = addressId + }; + + db.Insertable(new UnitCity() { AddressId = 1, Id = 1, Name = "a" }).ExecuteCommand(); + + int personId = db.Insertable(person).ExecuteReturnIdentity(); + + var list = db.Queryable().Includes(x => x.Persons).Includes(x=>x.City).ToList(); + db.UpdateNav(list) + .IncludeByNameString("Persons") + .IncludeByNameString("City").ExecuteCommand(); + } + + [SqlSugar.SugarTable("UnitPerson01x1")] + public class UnitPerson011 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public int AddressId { get; set; } + } + + [SqlSugar.SugarTable("UnitCityaa")] + public class UnitCity + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Name { get; set; } + public int AddressId { get; set; } + } + + [SqlSugar.SugarTable("UnitAddressx011")] + public class UnitAddress011 + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int Id { get; set; } + public string Street { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(UnitPerson011.AddressId))] + public List Persons { get; set; } + [SqlSugar.SugarColumn(IsNullable =true)] + public int CityId { get; set; } + [SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne, nameof(CityId))] + public UnitCity City { get; set; } + } + } +}