Parcourir la source

fix(control): 修复 D1-09 本地 SQLite 建表/schema 缺口(AUTOINCREMENT + 多表缺列)

1. HouseAutofocusCalibrationDB.id: long→int —— SQLite AUTOINCREMENT 只允许 INTEGER PRIMARY KEY,
   long→BIGINT 触发 'AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY' 致 InitTables 建表失败。
   id 仅作自增主键,业务从不按值读(查询按 tl_sn/house_sn/well_sn/scene),int 范围足够。
2. DBService.StartDbService:对 control 本地写的 9 个实体(TLSetting/House/HouseWellSetting/Dish/Balance/
   Embryo/HouseWellPhoto/Picture/HouseAutofocusCalibration)逐个 CodeFirst.InitTables 自愈本地 schema
   ——打包 aivfoTL.db schema 落后于实体(缺 localAutofocusEnabled 等列)致 DBUpdateTLInfo/UpdateHouseWellSetting
   报 'no such column'。InitTables 对已存在表只补缺失列(additive),各实体独立 try/catch。

真机验证:修复后 control 启动 DbException 条数=0(此前每启动报 AUTOINCREMENT + 多表 no such column);
started:true 正常驱动。6 单测仍过。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
huangjie il y a 3 jours
Parent
commit
b10bdce8c6

+ 5 - 1
ivf_tl_operate_2.0/control/ivf_tl_Entity/DBEntitys/HouseAutofocusCalibrationDB.cs

@@ -20,7 +20,11 @@ namespace IvfTl.Control.Entity.DBEntitys
 
         /// <summary>自增 id。</summary>
         [SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)]
-        public long id { get; set; }
+        // D1-09 修复:本地 SQLite 的 AUTOINCREMENT 只允许 INTEGER PRIMARY KEY;
+        // C# long → SQLite BIGINT 触发 "AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY"
+        // 致 CodeFirst.InitTables 建表失败。改 int(→SQLite INTEGER)即修。id 仅作自增主键,
+        // 业务从不按值读取(查询均按 tl_sn/house_sn/well_sn/scene),int 范围对标定记录足够。
+        public int id { get; set; }
 
         /// <summary>tl 设备 sn。</summary>
         [SugarColumn(ColumnName = "tl_sn")]

+ 16 - 6
ivf_tl_operate_2.0/control/ivf_tl_Services/DBService.cs

@@ -54,13 +54,23 @@ namespace IvfTl.Control.Services
                 // (M2-04 的标定镜像写入此表;本地库原无该表 → 写入失败无数据)。
                 // InitTables 发 CREATE TABLE IF NOT EXISTS,已建则不动;单独 try 包裹,
                 // 建表失败不阻断 DB 服务启动(与既有防御式风格一致)。
-                try
-                {
-                    Db.CodeFirst.InitTables(typeof(HouseAutofocusCalibrationDB));
-                }
-                catch (Exception exTable)
+                // D1-09 修复:本地 aivfoTL.db schema 落后于实体(多表缺列,如 tl_setting.localAutofocusEnabled、
+                // house_well_setting 缺列等)→ control 各 Update/Query 报 "no such column"。
+                // CodeFirst.InitTables 对已存在表只补缺失列(additive,ADD COLUMN,不删列不丢数据),逐实体自愈本地 schema;
+                // 每实体独立 try/catch,单表失败(如 SQLite ADD NOT NULL 列限制)不影响其它表与 DB 服务启动。
+                var localEntities = new[]
+                {
+                    typeof(HouseAutofocusCalibrationDB), typeof(TLSettingDB), typeof(HouseDB),
+                    typeof(HouseWellSettingDB), typeof(DishDB), typeof(BalanceDB),
+                    typeof(EmbryoDB), typeof(HouseWellPhotoDB), typeof(PictureDB)
+                };
+                foreach (var et in localEntities)
                 {
-                    ExceptionLogEvent?.Invoke(exTable, "DBServiceImpl.StartDbService.InitTables", null, LogEnum.DbException);
+                    try { Db.CodeFirst.InitTables(et); }
+                    catch (Exception exTable)
+                    {
+                        ExceptionLogEvent?.Invoke(exTable, "DBServiceImpl.StartDbService.InitTables:" + et.Name, null, LogEnum.DbException);
+                    }
                 }
 
                 return true;