导航更新,一对多,忽略主、子表字段时报错 返回
SqlSugar
处理完成
14
109

悬赏:0 飞吻
方法中的查询代码是这样的,实体类如下 public class SpotCheckPlanEntity : BaseEntity { /// <summary> /// 名称 /// </summary> [JsonPropertyOrder(1)] [SugarColumn(Length = 50)] public string Name { get; set; } /// <summary> /// 编号CP+日期+流水码 /// </summary> [JsonPropertyOrder(2)] [SugarColumn(Length = 50)] public string PlanNo { get; set; } . . . /// <summary> /// 备注 /// </summary> [JsonPropertyOrder(8)] [SugarColumn(Length = 200)] public string Remarks { get; set; } /// <summary> /// 点检项的导航属性 /// </summary> [Navigate(NavigateType.OneToMany, nameof(SpotCheckItemEntity.CheckPlanNo), nameof(PlanNo))] public List<SpotCheckItemEntity> SpotCheckItems { get; set; } } public class SpotCheckItemEntity { /// <summary> /// 数据库ID(uuid) /// </summary> [JsonPropertyOrder(-1)] [SugarColumn(IsPrimaryKey = true, Length = 50)] public string Id { get; set; } /// <summary> /// 点检计划编号 /// </summary> [JsonPropertyOrder(1)] [SugarColumn(Length = 50)] public string CheckPlanNo { get; set; } . . . /// <summary> /// 备注 /// </summary> [JsonPropertyOrder(8)] [SugarColumn(Length = 100, IsNullable = true)] public string Remarks { get; set; } } 查询方法如下: internal static ResponseData<RspSpotCheckPlan> UpdeteSpotCheckPlan(SpotCheckPlanEntity entity, string currentName) { Expression<Action<IUpdateable<SpotCheckPlanEntity>>> rootFunc = it => it .IgnoreColumns(x => x.CreatedBy) .IgnoreColumns(x => x.CreatedTime) .IgnoreColumns(x => x.PlanNo); Expression<Action<IUpdateable<SpotCheckItemEntity>>> currentFunc = it => it .IgnoreColumns(x => x.Id) .IgnoreColumns(x => x.CheckPlanNo); var result = DBHelper.Db(currentName).UpdateNav(entity).Include(x => x.SpotCheckItems, new UpdateNavOptions() { RootFunc = rootFunc, CurrentFunc = currentFunc, }).ExecuteCommand(); } 查询中,我试图用两个委托分别去忽略两个实体类的某些字段,其中字段都是真实存在的,只是前端不提供他们的修改值,所以我想不更新这些字段,但运行时 报了一个语法错误。而且错误毫无参考价值。打印的sql走偏的厉害。贵网也没有相关文档描述,是不是不支持这样的操作去忽略字段呢,我尝试用它的根选择器 去忽略主表字段,但也不行。 请问这样写有什么错
热忱回答(14)
-
fate sta VIP0
2周前.IgnoreColumns(x => x.Id) 主键不能忽略
0 回复 -
fate sta VIP0
2周前你这个问题需要提供完整DEMO
0 回复 -
fate sta VIP0
2周前https://www.donet5.com/Home/Doc?typeId=2366 按提问模版 提供完整可以跑的DEMO
0 回复 -
树先生 VIP0
2周前@fate sta:好,相关demo我后面提供,是不是因为我忽略到了ID或者导航属性导致的,排除我上面的使用还算正确的情况下
0 回复 -
fate sta VIP0
2周前@树先生:看着没什么问题除了多忽略了主键有DEMO最好
0 回复 -
树先生 VIP0
2周前@fate sta:
[HttpGet] public object Get() { var db = new SqlSugarScope(new SqlSugar.ConnectionConfig() { ConnectionString = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST", DbType = DbType.SqlServer, IsAutoCloseConnection = true }); //建表 if (!db.DbMaintenance.IsAnyTable("Test001", false)) { db.CodeFirst.InitTables<Test002>(); db.CodeFirst.InitTables<Test003>(); } Test003 test003 = new Test003() { CheckPlanNo = "123456", Remarks = "hahah", Id = null }; List<Test003> test003s = new List<Test003>(); test003s.Add(test003); Test002 entity = new Test002() { Name = "haha", Id = "12", PlanNo = "123456", SpotCheckItems = test003s }; Expression<Action<IUpdateable<Test002>>> rootFunc = it => it .IgnoreColumns(x => x.CreatedBy) .IgnoreColumns(x => x.CreatedTime) .IgnoreColumns(x => x.PlanNo); Expression<Action<IUpdateable<Test003>>> currentFunc = it => it .IgnoreColumns(x => x.Id) .IgnoreColumns(x => x.CheckPlanNo); var result = DBHelper.Db().UpdateNav(entity).Include(x => x.SpotCheckItems, new UpdateNavOptions() { RootFunc = rootFunc, CurrentFunc = currentFunc, }).ExecuteCommand(); return result; } } public class Test001 { /// <summary> /// 数据库ID(uuid) /// </summary> [JsonPropertyOrder(-1)] [SugarColumn(IsPrimaryKey = true, Length = 50)] public string Id { get; set; } /// <summary> /// 创建时间 /// </summary> [JsonPropertyOrder(100)] [SugarColumn(IsNullable = true)] public DateTime CreatedTime { set; get; } = DateTime.Now; /// <summary> /// 更新时间 /// </summary> [JsonPropertyOrder(102)] [SugarColumn(IsNullable = true)] public DateTime? UpdatedTime { set; get; } /// <summary> /// 创建者 /// </summary> [JsonPropertyOrder(101)] [SugarColumn(IsNullable = true)] public string CreatedBy { set; get; } /// <summary> /// 更新者 /// </summary> [JsonPropertyOrder(103)] [SugarColumn(IsNullable = true)] public string? UpdatedBy { set; get; } } public class Test002:Test001 { /// <summary> /// 名称 /// </summary> [JsonPropertyOrder(1)] [SugarColumn(Length = 50)] public string Name { get; set; } /// <summary> /// 计划ID /// </summary> [JsonPropertyOrder(2)] [SugarColumn(Length = 50)] public string PlanNo { get; set; } /// <summary> /// 导航属性 /// </summary> [Navigate(NavigateType.OneToMany, nameof(SpotCheckItemEntity.CheckPlanNo), nameof(PlanNo))] public List<Test003> SpotCheckItems { get; set; } } public class Test003 { /// <summary> /// 数据库ID(uuid) /// </summary> [JsonPropertyOrder(-1)] [SugarColumn(IsPrimaryKey = true, Length = 50)] public string Id { get; set; } /// <summary> /// 计划编号 /// </summary> [JsonPropertyOrder(1)] [SugarColumn(Length = 50)] public string CheckPlanNo { get; set; } /// <summary> /// 备注 /// </summary> [JsonPropertyOrder(8)] [SugarColumn(Length = 100, IsNullable = true)] public string Remarks { get; set; } } 你看这个模板可以不
0 回复 -
fate sta VIP0
2周前0 回复 -
fate sta VIP0
2周前这个例子你自已都没测过吧
0 回复 -
fate sta VIP0
2周前0 回复 -
fate sta VIP0
2周前还有这个还是错的
0 回复 -
fate sta VIP0
2周前你自已搞个DEMO测试完能重现,在发出来
0 回复 -
树先生 VIP0
2周前@fate sta:测试后我发现这个错是因为前端没有给导航属性的值(因为我的导航属性是非ID,前端可以不传),主子表导航属性为Null时,导航更新是会报错的,不知道是不是这个问题
0 回复 -
fate sta VIP0
2周前@树先生:你搞能跑的DEMO光说,我也不知道你怎么写的
0 回复 -
树先生 VIP0
2周前@fate sta:已经可以了,就是导航属性为null时更新失败的问题
0 回复