V1.12.1: base_device 实体 NodeId 误标主键修复 + SyncDevicesAsync 诊断日志

【问题】
主人反馈:base_device 后端无法保存数据到数据库(A3 设备同步静默不写但返回 200)。

【根因】
base_device.cs:266-274 NodeId 字段被误标:
  [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
  [Key]
  public int? NodeId { get; set; }
而 DeviceId 已经是主键+自增(line 23-29),产生 3 重故障:
1. SqlSugar INSERT 时同时跳过 DeviceId 和 NodeId,但数据库 NodeId INT NULL 不是 AUTO_INCREMENT
   → entity.NodeId=gatewayNodeId 被跳过,实际写 NULL/默认值
2. Updateable WHERE 子句变成 DeviceId=X AND NodeId=Y 双重主键匹配
   → NodeId 实际值与赋的值不一致时,Update 影响行数=0 静默失败
3. SqlSugar 异常被 SyncDevicesAsync catch 块吞掉,Controller 看不到具体原因

【修复】
1. base_device.cs:268-270 移除 [SugarColumn(IsPrimaryKey=true,IsIdentity=true)] 和 [Key]
   NodeId 改为普通外键字段(只保留 Column/Editable)
2. gateway_nodesService.cs: SyncDevicesAsync 新增 INSERT try/catch + 详细日志
   - 每条新增/更新打印 Adapter/SourceId/DeviceId/NodeId 便于定位
   - 失败时记录 entity 完整字段值(Name/Cat/Grp)便于排查
   - catch 块 LogError 后 throw,异常不再被吞

【验证】
dotnet build api_sqlsugar 0 错误 0 警告

【效果】
- Insertable 正常写入 entity.NodeId=gatewayNodeId 值
- Updateable WHERE 只用 DeviceId 单主键匹配
- 异常不再静默,Controller 会返回 500 + 具体异常信息,主人能从日志看到根因
This commit is contained in:
2026-07-24 05:47:47 +08:00
parent 3d41f4d412
commit 2315d5a174
3 changed files with 30 additions and 8 deletions
@@ -266,9 +266,12 @@ namespace VolPro.Entity.DomainModels
/// <summary>
///所属网关节点ID
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
[Key]
[Display(Name ="所属网关节点ID")]
// V1.12.1 修复:NodeId 不是主键,只是一个外键字段。
// 之前误标 [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [Key]
// 会导致 SqlSugar INSERT 时跳过 NodeId(认为是自增)→ 数据库实际为 NULL,
// 传进来的 gatewayNodeId 永远写不进去;Update 时 WHERE 变成双主键匹配,
// 容易因 NodeId 实际值与赋的值不一致导致 Update 静默失败。
[Column(TypeName="int")]
[Editable(true)]
public int? NodeId { get; set; }