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);