From 478dac6ae8be9215d9643ee2c280cec5e748538f Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 25 Jun 2025 17:36:41 +0800 Subject: [PATCH] Add demo --- .../UserTestCases/UnitTest/Main.cs | 1 + .../UserTestCases/UnitTest/Unitsfasdyd.cs | 184 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitsfasdyd.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs index e18448a8a..e244fea50 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs @@ -33,6 +33,7 @@ namespace OrmTest } public static void Init() { + Unitsfasdyd.Init(); Unitafdsafsss.Init(); Unitdfdaysss.Init(); Unitdsfsssysf.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitsfasdyd.cs b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitsfasdyd.cs new file mode 100644 index 000000000..718b80738 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitsfasdyd.cs @@ -0,0 +1,184 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + internal class Unitsfasdyd + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + + db.DbMaintenance.TruncateTable(); + + var dept = new DeptEntity() + { + Id = Guid.NewGuid(), + DeptName = "研发部", + DeptCode = "RD", + Leader = "张三" + }; + + db.Insertable(dept).ExecuteCommand(); + + var user = new UserEntity() + { + Id = Guid.NewGuid(), + UserName = "admin", + DeptId = dept.Id, // 关联部门 + EncryPassword = new EncryPasswordValueObject("123qwe"), + }; + + + db.Insertable(user).ExecuteCommand(); + + var users = db.Queryable() + //.Includes(x => x.Roles.Where(r=>r.RoleName.StartsWith("管理")).ToList()) + .Includes(x => x.Dept) + .ToList(); + + if (users.First().Dept == null) + { + throw new Exception("error"); + } + } + + /// + /// 角色表 + /// + [SugarTable("sys_roledsfadf222")] + public class RoleEntity + { + [SugarColumn(IsPrimaryKey = true)] + public Guid Id { get; set; } + /// + /// 角色名 + /// + public string RoleName { get; set; } = string.Empty; + + /// + /// 角色编码 + /// + [SugarColumn(ColumnName = "RoleCode")] + public string RoleCode { get; set; } = string.Empty; + + /// + /// 描述 + /// + [SugarColumn(ColumnName = "Remark")] + public string Remark { get; set; } + } + + + /// + /// 用户表 + /// + [SugarTable("sys_user1231313131")] + [SugarIndex($"index_{nameof(UserName)}", nameof(UserName), OrderByType.Asc)] + public class UserEntity + { + [SugarColumn(IsPrimaryKey = true)] + public Guid Id { get; set; } + + /// + /// 用户名 + /// + public string UserName { get; set; } = string.Empty; + + + /// + /// 加密密码 + /// + [SugarColumn(IsOwnsOne = true)] + public EncryPasswordValueObject? EncryPassword { get; set; } + + + ///// + ///// 角色 + ///// + //[Navigate(typeof(UserRoleEntity), nameof(UserRoleEntity.UserId), nameof(UserRoleEntity.RoleId))] + //public List Roles { get; set; } + + + /// + /// 部门id + /// + public Guid? DeptId { get; set; } + + /// + /// 部门 + /// + + [Navigate(NavigateType.OneToOne, nameof(DeptId))] + public DeptEntity? Dept { get; set; } + + } + + [SugarTable("sys_dept2213s")] + public class DeptEntity + { + [SugarColumn(IsPrimaryKey = true)] + public Guid Id { get; set; } + + /// + /// 部门名称 + /// + public string DeptName { get; set; } + /// + /// 部门编码 + /// + [SugarColumn(ColumnName = "DeptCode")] + public string DeptCode { get; set; } + /// + /// 负责人 + /// + [SugarColumn(ColumnName = "Leader")] + public string? Leader { get; set; } + } + + [SugarTable("sys_userrole111")] + public class UserRoleEntity + { + [SugarColumn(IsPrimaryKey = true)] + public Guid Id { get; set; } + + + /// + /// 角色id + /// + public Guid RoleId { get; set; } + + /// + /// 用户id + /// + public Guid UserId { get; set; } + } + + public class EncryPasswordValueObject + { + public EncryPasswordValueObject() { } + public EncryPasswordValueObject(string password) + { + this.Password = password; + this.Salt = DateTime.Now.Ticks.ToString(); // 使用当前时间戳作为盐值 + } + + /// + /// 密码 + /// + public string Password { get; set; } = string.Empty; + + /// + /// 加密盐值 + /// + public string Salt { get; set; } = string.Empty; + + + } + } +}