From 9a99a5fc4e1a27b54a8a6e2404d5d9bdb33a363e Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 30 Jul 2022 18:04:29 +0800 Subject: [PATCH] Add unit test --- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 1 + .../UnitTest/UnitManyToManyUpdate.cs | 79 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/UnitManyToManyUpdate.cs diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index 91b6aa48d..e6d6b6dae 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 1885db7c5..545951f3d 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() { + UnitManyToManyUpdate.Init(); UnitManyToMay1231.Init(); UnitUpdateNav2.Init(); UnitUpdateNav.Init(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UnitManyToManyUpdate.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UnitManyToManyUpdate.cs new file mode 100644 index 000000000..dec2c6b01 --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UnitManyToManyUpdate.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using SqlSugar; +using static OrmTest.Program; +namespace OrmTest +{ + public class UnitManyToManyUpdate + { + public static void Init() + { + var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + }); + db.Aop.OnLogExecuting = (sql, pars) => + { + Console.WriteLine(sql); + }; + //建表 + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + //插入数据 + db.Insertable(new SchoolA111 { SchoolId = 1, SchoolName = "清华" }).ExecuteCommand(); + db.Insertable(new SchoolAndRoomA111 { SchoolId = 1, RoomId = 1, Weight = 1 }).ExecuteCommand(); + db.Insertable(new SchoolAndRoomA111 { SchoolId = 1, RoomId = 2, Weight = 1 }).ExecuteCommand(); + db.Insertable(new RoomA111 { RoomId = 1, RoomName = "甲号房" }).ExecuteCommand(); + db.Insertable(new RoomA111 { RoomId = 2, RoomName = "乙号房" }).ExecuteCommand(); + //用例代码 + //查关系表数据 + var data = db.Queryable() + .Includes(it => it.School) + .Includes(it => it.Room) + .First(); + //修改A表或B表数据 + data.Room.RoomName = "丙号房"; + //执行导航更新 + //报错Sql (Update关系表中的AB表主键): + // UPDATE [SchoolAndRoomA] SET [RoomId]=1 WHERE SchoolId = 1 + var result = db.UpdateNav(data) + .Include(it => it.School) + .Include(it => it.Room) + .ExecuteCommand(); + Console.WriteLine(result); + Console.WriteLine("用例跑完"); + Console.ReadKey(); + } + //建类 + public class SchoolA111 + { + [SugarColumn(IsPrimaryKey = true)] + public int SchoolId { get; set; } + public string SchoolName { get; set; } + } + public class SchoolAndRoomA111 + { + [SugarColumn(IsPrimaryKey = true)] + public int SchoolId { get; set; } + [SugarColumn(IsPrimaryKey = true)] + public int RoomId { get; set; } + public int? Weight { get; set; } + [SugarColumn(IsNullable = true)] + [Navigate(NavigateType.OneToOne, nameof(SchoolId))] + public SchoolA111 School { get; set; } + [SugarColumn(IsNullable = true)] + [Navigate(NavigateType.OneToOne, nameof(RoomId))] + public RoomA111 Room { get; set; } + } + public class RoomA111 + { + [SugarColumn(IsPrimaryKey = true)] + public int RoomId { get; set; } + public string RoomName { get; set; } + } + } +} \ No newline at end of file