So I'm currently developing a WPF app which is linked to a database, and I've got a method in which I have a for-each loop that iterates through each item added to my basket and adds the items to the database so that I can print a receipt.
When I have an item in the middle of the basket, that has been sold out, I want to stop the for-each loop (not iterate in any other items) and display a message explaining that the item is out of stock, so that the user can edit the basket and remove that item, and then proceed to print the receipt again.
When this case happens, I get an error because the receipt ID and some of the items have already been Added (dbContext.Add(item)
) so when the user clicks print receipt again (After amending the sold-out item), when I call save, I've got added data on the entity, that has not been added.
Is there any way of detaching everything that has not been saved if one item is out of stock ??
Error Message:
UpdateException: Unable to determine the principal end of the 'Context.FK_Receipt_Item_Receipt' relationship. Multiple added entities may have the same primary key.
public void AddToDatabase()
{
boolean canPrintReceipt = true;
Receipt receipt= new Receipt
{
ID = Guid.NewGuid()
};
Db.Receipts.Add(receipt);
foreach (KeyValuePair<string, int> entry in Basket)
{
Item item= new Item();
if // item is out of stock
{
canPrintReceipt = false;
break;
}
else //Add new Item
{
...
...
Db.Items.Add(item);
}
Receipt_Item receiptItems = new Receipt_Jugada
{
ReceiptID = receip.ID,
ItemID = item.ID,
...,
...
};
Db.Receipt_Items.Add(receiptItems);
}
if (canPrintReceipt)
{
Db.SaveChanges();
Basket.Clear();
}
}