using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; namespace OrmTest { internal class UnitManyToMany4 { public static void Init() { var db = NewUnitTest.Db; db.CodeFirst.InitTables(); db.DbMaintenance.TruncateTable(); db.Insertable(new OperatorInfo() { id="1", createTime=DateTime.Now, isDel=1, isDisabled=1, openid="", phone="", pwd="", realname="a01", remark="a", sno="a", username="a01" }).ExecuteCommand(); db.Insertable(new OperatorInfo() { id = "2", createTime = DateTime.Now, isDel = 1, isDisabled = 1, openid = "", phone = "", pwd = "", realname = "a01", remark = "a", sno = "a", username = "admin" }).ExecuteCommand(); db.Insertable(new OperatorInfo() { id = "3", createTime = DateTime.Now, isDel = 1, isDisabled = 1, openid = "", phone = "", pwd = "", realname = "a01", remark = "a", sno = "a", username = "admin" }).ExecuteCommand(); var id=db.Insertable(new Role() { id=1, createTime=DateTime.Now, name="admin1" }).ExecuteReturnIdentity(); var id2 = db.Insertable(new Role() { id = 2, createTime = DateTime.Now, name = "admin2" }).ExecuteReturnIdentity(); db.InsertNav(new OperatorInfo() { id = "1113", createTime = DateTime.Now, isDel = 1, isDisabled = 1, openid = "", phone = "", pwd = "", realname = "a01", remark = "a", sno = "a", username = "admin", Roles = new List() { new Role() { id = 2 } } }).Include(z => z.Roles, new InsertNavOptions() { ManyToManySaveMappingTemplate = new OptRole() { CreateTime = "2020", OrgId ="1x" } }) .ExecuteCommand(); var list=db.Queryable() .Where(x=>x.id== "1113").Includes(x => x.Roles).ToList(); var mapping = db.Queryable().Where(x => x.operId == "1113").First(); if (mapping.OrgId != "1x"||mapping.CreateTime!="2020"|| list.First().Roles.Count!=1) { throw new Exception("unit error"); } db.UpdateNav(new OperatorInfo() { id = "1113", createTime = DateTime.Now, isDel = 1, isDisabled = 1, openid = "", phone = "", pwd = "", realname = "a01", remark = "a", sno = "a", username = "admin", Roles = new List() { new Role() { id = 2 } } }).Include(z => z.Roles, new UpdateNavOptions() { ManyToManySaveMappingTemplate = new OptRole() { CreateTime = "1010", OrgId = "1x" } }) .ExecuteCommand(); } /// /// 描述: /// 作者:synjones /// 时间:2022-04-20 21:30:28 /// [SugarTable("unit_operatorinfoasdfa31")] public partial class OperatorInfo { /// /// 多角色 /// [Navigate(typeof(OptRole), nameof(OptRole.operId), nameof(OptRole.roleId))]//名字换 public List Roles { get; set; } /// /// 主键 /// [SugarColumn(IsPrimaryKey = true, ColumnName = "MYID")] public string id { get; set; } /// /// 姓名 /// public string realname { get; set; } /// /// 账号 /// public string username { get; set; } /// /// 密码 /// public string pwd { get; set; } /// /// 学号 /// public string sno { get; set; } /// /// openid /// public string openid { get; set; } /// /// 手机号码 /// public string phone { get; set; } /// /// 备注信息 /// public string remark { get; set; } /// /// 创建日期 /// public DateTime createTime { get; set; } /// /// 状态(1:启用,2:禁用) /// public int isDisabled { get; set; } /// /// 是否删除(1:正常;2:删除) /// public int isDel { get; set; } } /// /// 描述: /// 作者:synjones /// 时间:2022-04-20 21:30:28 /// [SugarTable("unit_role1adsfa")] public partial class Role { /// /// 角色 /// [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "MYID")] public int id { get; set; } /// /// 角色名称 /// public string name { get; set; } /// /// 创建时间 /// public DateTime createTime { get; set; } } /// /// 描述: /// 作者:synjones /// 时间:2022-04-21 14:35:09 /// [SugarTable("unit_operator_roleadfaa")] public partial class OptRole { /// /// /// [SugarColumn(IsPrimaryKey = true,ColumnName ="MYID")] public long id { get; set; } /// /// /// [SugarColumn(ColumnName="AID")] public string operId { get; set; } [SugarColumn(ColumnName = "bid")] public int roleId { get; set; } [SugarColumn(ColumnName = "ct")] public string CreateTime { get; set; } [SugarColumn(ColumnName = "oid")] public string OrgId { get; set; } } } }