Fix new barcode not saved when adding existing item to transaction

This commit is contained in:
James Chapman 2023-11-12 17:13:40 +00:00
parent 60c48ce952
commit cd77aaf313
Signed by: jamsch0
GPG Key ID: 765FE58130277547
2 changed files with 8 additions and 2 deletions

View File

@ -10,6 +10,7 @@ public class AppDbContext : DbContext
}
public DbSet<Item> Items => Set<Item>();
public DbSet<ItemBarcode> ItemBarcodes => Set<ItemBarcode>();
public DbSet<ItemPurchase> ItemPurchases => Set<ItemPurchase>();
public DbSet<ItemTagQuantity> ItemTagQuantities => Set<ItemTagQuantity>();
public DbSet<List> Lists => Set<List>();
@ -28,7 +29,6 @@ public class AppDbContext : DbContext
modelBuilder.Entity<ItemBarcode>(entity =>
{
entity.ToTable("item_barcodes");
entity.HasKey(e => new { e.ItemId, e.BarcodeData });
entity.Property(e => e.Format)

View File

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