using SqlSugar; using System.Linq.Expressions; using System.Text; using System; using System.Collections.Generic; using System.Linq; namespace OrmTest { public class UnitInsertNav { public static void Init() { //生成 DI 容器 var context = NewUnitTest.Db; ; context.Aop.DataExecuting = (oldValue, entityInfo) => { if (entityInfo.PropertyName == "Iden" && entityInfo.OperationType == DataFilterType.InsertByObject) { entityInfo.SetValue(Guid.NewGuid().ToString("N").ToUpper()); } }; //建表 if (!context.DbMaintenance.IsAnyTable("MENU_INFO_1", false)) { context.CodeFirst.InitTables(); } //建表 if (!context.DbMaintenance.IsAnyTable("ACTIVITY_RULE_1", false)) { context.CodeFirst.InitTables(); } context.DbMaintenance.TruncateTable(); var result = context.Insertable(new ActivityRule() { MenuIden = "1", RuleContent = "1" }).ExecuteReturnEntity (); MenuInfo menuInfo = new MenuInfo { MenuCode = "2", ActivityRules = new List { new ActivityRule() { Iden = result.Iden, RuleContent = "2" } } }; Console.WriteLine("begin insert nav"); context.InsertNav(menuInfo) .Include(s => s.ActivityRules) .ExecuteCommand (); var list = context.Queryable() .Includes(x => x.ActivityRules) .ToList(); if (list.First().ActivityRules.Count() == 0) { throw new Exception("unit error"); } } /// /// /// [SugarTable("MENU_INFO_1")] public partial class MenuInfo { public MenuInfo() { } /// /// Desc:唯一编号 /// Default: /// Nullable:False /// [SugarColumn(IsPrimaryKey = true, ColumnName = "IDEN")] public string Iden { get; set; } /// /// Desc:菜单编号 /// Default: /// Nullable:True /// [SugarColumn(ColumnName = "MENU_CODE")] public string MenuCode { get; set; } [SugarColumn(IsIgnore = true)] [Navigate(NavigateType.OneToMany, nameof(ActivityRule.MenuIden))] public List ActivityRules { get; set; } } /// ///组织信息 /// [SugarTable("ACTIVITY_RULE_1")] public partial class ActivityRule { public ActivityRule() { } /// /// uuid /// [SugarColumn(IsPrimaryKey = true, ColumnName = "IDEN")] public string Iden { get; set; } /// /// 菜单IDEN /// [SugarColumn(ColumnName = "MENU_IDEN")] public string MenuIden { get; set; } /// /// uuid /// [SugarColumn(ColumnName = "RUlE_CONTENT")] public string RuleContent { get; set; } } } }