From 00e17497bf9092842328d2b710bc1cab9f7b900c Mon Sep 17 00:00:00 2001 From: James Chapman Date: Sun, 13 Oct 2024 16:17:35 +0100 Subject: [PATCH] Fix error saving transaction with a promotion with items --- Groceries/Transactions/TransactionsController.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Groceries/Transactions/TransactionsController.cs b/Groceries/Transactions/TransactionsController.cs index cdcd990..0608783 100644 --- a/Groceries/Transactions/TransactionsController.cs +++ b/Groceries/Transactions/TransactionsController.cs @@ -243,11 +243,13 @@ public class TransactionsController : Controller using var dbContext = dbContextFactory.CreateDbContext(); + foreach (var item in transaction.Items) + { + item.Item = null; + } + // Work around EF trying to insert items by explicitly tracking them as unchanged - dbContext.Items.AttachRange( - transaction.Items - .Select(item => item.Item!) - .Concat(transaction.Promotions.SelectMany(promotion => promotion.Items))); + dbContext.Items.AttachRange(transaction.Promotions.SelectMany(promotion => promotion.Items)); dbContext.Transactions.Add(transaction); await dbContext.SaveChangesAsync();