Fix error saving transaction with a promotion with items

This commit is contained in:
James Chapman 2024-10-13 16:17:35 +01:00
parent 2c0f6f1cab
commit 00e17497bf
Signed by: jamsch0
GPG Key ID: 765FE58130277547

View File

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