From cd77aaf313710172bb96ed64bbf78cdaa6317008 Mon Sep 17 00:00:00 2001 From: James Chapman Date: Sun, 12 Nov 2023 17:13:40 +0000 Subject: [PATCH] Fix new barcode not saved when adding existing item to transaction --- Groceries.Data/AppDbContext.cs | 2 +- Groceries/Transactions/TransactionsController.cs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Groceries.Data/AppDbContext.cs b/Groceries.Data/AppDbContext.cs index 2c28ba9..c952f8b 100644 --- a/Groceries.Data/AppDbContext.cs +++ b/Groceries.Data/AppDbContext.cs @@ -10,6 +10,7 @@ public class AppDbContext : DbContext } public DbSet Items => Set(); + public DbSet ItemBarcodes => Set(); public DbSet ItemPurchases => Set(); public DbSet ItemTagQuantities => Set(); public DbSet Lists => Set(); @@ -28,7 +29,6 @@ public class AppDbContext : DbContext modelBuilder.Entity(entity => { - entity.ToTable("item_barcodes"); entity.HasKey(e => new { e.ItemId, e.BarcodeData }); entity.Property(e => e.Format) diff --git a/Groceries/Transactions/TransactionsController.cs b/Groceries/Transactions/TransactionsController.cs index 77377e9..5389cf1 100644 --- a/Groceries/Transactions/TransactionsController.cs +++ b/Groceries/Transactions/TransactionsController.cs @@ -142,7 +142,13 @@ public class TransactionsController : Controller var item = new Item(itemId, brand, name); if (barcodeData != null && barcodeFormat != null) { - item.Barcodes.Add(new ItemBarcode(itemId, barcodeData.Value, barcodeFormat)); + var barcode = new ItemBarcode(itemId, barcodeData.Value, barcodeFormat); + item.Barcodes.Add(barcode); + + if (!await dbContext.ItemBarcodes.ContainsAsync(barcode)) + { + dbContext.ItemBarcodes.Add(barcode); + } } dbContext.Items.Attach(item);