STAGE 3-4

This commit is contained in:
2025-11-12 18:57:51 +08:00
parent 744eddcd77
commit ae09a6f487
17 changed files with 80 additions and 35 deletions

View File

@@ -23,6 +23,12 @@ namespace Model
public virtual ICollection<actItem_files> actItem_files { get; set; }
}
// TODO: CRITICAL - 靜態字段設計問題(同 follower.cs
// 權衡AsEnumerable() vs ToList()
// - AsEnumerable(): 不立即佔內存,但 DbContext 未 Dispose有風險
// - ToList(): 立即載入所有 files 到內存(可能很大)且數據過時
// 建議:改為實例方法或使用緩存機制
// 暫時保留 AsEnumerable() 避免立即載入大量數據
public static IEnumerable<Model.file> allFiles = new Model.ezEntities().files.AsEnumerable();
}
}

View File

@@ -75,6 +75,22 @@ namespace Model
Seeker = 10,
}
// TODO: CRITICAL - 靜態字段設計問題
// 【現狀問題】
// 1. 靜態字段會在類加載時創建新的 DbContext但從未 Dispose
// 2. ToList() 會立即載入所有 followers 到內存(可能數千筆),佔用大量內存且數據會過時
// 3. AsEnumerable() 看似更好(延遲執行),但會導致 DbContext 生命週期問題
//
// 【權衡】
// - 用 AsEnumerable(): 不立即佔內存,但 DbContext 未 Dispose後續訪問可能出錯
// - 用 ToList(): DbContext 可安全關閉,但立即佔用大量內存且數據過時
//
// 【建議改法】
// 方案 A: 改為實例方法 `public static List<follower> GetAllFollowers() { using(var db = new ezEntities()) return db.followers.ToList(); }`
// 方案 B: 使用緩存機制MemoryCache定期更新
// 方案 C: 完全移除此字段,改為按需查詢
//
// 暫時保留 AsEnumerable() 避免立即載入大量數據(雖然有 DbContext 問題,但至少不會內存爆掉)
public static IEnumerable<Model.follower> allFaollowers = new Model.ezEntities().followers.AsEnumerable();
//public static string identity_type_list()

View File

@@ -151,7 +151,7 @@ namespace Model
.Where(q => q.actItem_num == this.actItem_num) // 同品項
.Where(q => q.print_id != null) // Ensure print_id is not null
.Select(q => q.print_id) // Just select the print_id
.AsEnumerable() // Switch to in-memory operations
.AsEnumerable() // ✅ 合理使用EF 無法轉換 StartsWith/Contains/Replace 到 SQL
.Where(q => q.StartsWith(parent_print_id)) // Now do string operations in memory
.Where(q => q.Contains(itemPrefix))
.Select(q => q.Trim().Replace("\t", ""))