currently i am trying to solve the riddle of EntityFramework magically altering my property during SaveChanges
so for some reason, the following snipped decides to alter the value of ReceiptEntry
to always 10 or 12
void HandleReceipt(DBContext cntxt, Receipt receipt, ...)
{
[...]
var receiptEntries = receipt.GetEntries(cntxt);
var receiptEntriesItem = from q in cntxt.Items
where ...
select new { ... };
var receiptEntriesItemDictionary = receiptEntriesItem.ToDictionary(...);
[...]
while (...)
{
[...]
var receiptEntry = receiptEntriesItemDictionary[it.ItemFK];
[...]
foreach (var kvp in ...)
{
var sle = usedStorages.First((sl) => sl.Id == kvp.Key);
entry = new PickListEntry
{
Parent = receipt.PickList,
Amount = kvp.Value,
ReceiptEntry = receiptEntry, // receiptEntry is the instance with eg. Id 60
StorageLocation = sle
};
sle.Stock = new Stock { AmountAvailable = 0, ItemFK = receiptEntry.ItemFK };
var createdEntry = cntxt.PickListEntries.Add(entry);
cntxt.SaveChanges(); // ReceiptEntry property gets modified in here to the instance with 10 or 12
[...]
}
[...]
}
}
by now, i am clueless of why this may happens.
The 10 & 12 are entries that do not relate to the currenly worked on receipt
in any way (however, they are loaded in the current cntxt
and processed in another call to HandleReceipt
)
I already tripple checked that theese two never are assigned anywhere (entry is only ever used right there and never afterwards in here) and checked when they get changed (The famous [External Code]
is exactly one below the setter)
Anybody capable of telling me why this is happening and how to fix it? Spinning up another DBContext
is no option as this is happening inside a transaction (cntxt.Database.BeginTransaction()
) (aka: something fails means everything needs to be rolled back)