Files
SecMPS/api_sqlsugar
g82tt 2315d5a174 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 + 具体异常信息,主人能从日志看到根因
2026-07-24 05:48:18 +08:00
..
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00
2026-05-15 23:22:48 +08:00