Fix: Complete workaround for EF Core 9 + SQLite GUID parameter bug - load all variants then filter in-memory

This commit is contained in:
SysAdmin 2025-10-05 17:01:52 +01:00
parent 45da991945
commit 76707eb565

View File

@ -25,32 +25,20 @@ public class ProductService : IProductService
.Where(p => p.IsActive)
.ToListAsync();
Console.WriteLine($"[DEBUG] Loaded {products.Count} products");
// Manually load variants - using fresh query context to avoid Contains() json_each bug in EF Core 9 + SQLite
// Load ALL active variants first, then filter in memory
var allVariants = await _context.ProductVariants
.Where(v => v.IsActive)
.ToListAsync();
Console.WriteLine($"[DEBUG] Loaded {allVariants.Count} total variants from database");
// Filter to only variants that belong to loaded products
var productIds = products.Select(p => p.Id).ToHashSet();
var matchingVariants = allVariants.Where(v => productIds.Contains(v.ProductId)).ToList();
Console.WriteLine($"[DEBUG] Filtered to {matchingVariants.Count} variants matching {productIds.Count} products");
foreach (var v in matchingVariants.Take(5))
{
Console.WriteLine($"[DEBUG] - Variant: {v.Id}, ProductId: {v.ProductId}, Type: {v.VariantType}, Name: {v.Name}");
}
// Group variants by ProductId for quick lookup
var variantsByProduct = matchingVariants.GroupBy(v => v.ProductId)
.ToDictionary(g => g.Key, g => g.ToList());
Console.WriteLine($"[DEBUG] Grouped variants into {variantsByProduct.Count} product groups");
return products.Select(p => new ProductDto
{
Id = p.Id,
@ -192,25 +180,13 @@ public class ProductService : IProductService
if (product == null) return null;
// Manually load variants - using fresh query to avoid Contains() json_each bug in EF Core 9 + SQLite
// Load ALL variants first to debug
// Manually load variants - work around EF Core 9 + SQLite GUID parameter comparison bug
// Must load ALL variants then filter in-memory - WHERE clause with GUID parameters doesn't work reliably
var allVariants = await _context.ProductVariants.ToListAsync();
Console.WriteLine($"[DEBUG GetProductById] Total variants in DB: {allVariants.Count}");
Console.WriteLine($"[DEBUG GetProductById] Looking for ProductId: {id} (Type: {id.GetType().Name})");
var matchingByProductId = allVariants.Where(v => v.ProductId == id).ToList();
Console.WriteLine($"[DEBUG GetProductById] Matching by ProductId: {matchingByProductId.Count}");
var matchingByIsActive = allVariants.Where(v => v.IsActive).ToList();
Console.WriteLine($"[DEBUG GetProductById] Matching by IsActive: {matchingByIsActive.Count}");
var variants = allVariants.Where(v => v.ProductId == id && v.IsActive).OrderBy(v => v.SortOrder).ToList();
Console.WriteLine($"[DEBUG GetProductById] Final variants loaded: {variants.Count}");
foreach (var v in variants.Take(3))
{
Console.WriteLine($"[DEBUG GetProductById] - {v.VariantType}: {v.Name}, ProductId: {v.ProductId}");
}
var variants = allVariants
.Where(v => v.ProductId == id && v.IsActive)
.OrderBy(v => v.SortOrder)
.ToList();
return new ProductDto
{