I write many (20+) parent data sets for children to the database, and EF requires me to save the changes between each set, without which he complains that he will not be able to determine the primary key. Can data be flushed to SQL Server so that EF can return the primary keys from the identifiers and SaveChanges are sent at the end of the record of all changes?
foreach (var itemCount in itemCounts) { var addItemTracking = new ItemTracking { availabilityStatusID = availabilityStatusId, itemBatchId = itemCount.ItemBatchId, locationID = locationId, serialNumber = serialNumber, trackingQuantityOnHand = itemCount.CycleQuantity }; _context.ItemTrackings.Add(addItemTracking); _context.SaveChanges(); var addInventoryTransaction = new InventoryTransaction { activityHistoryID = newInventoryTransaction.activityHistoryID, itemTrackingID = addItemTracking.ItemTrackingID, personID = newInventoryTransaction.personID, usageTransactionTypeId = newInventoryTransaction.usageTransactionTypeId, transactionDate = newInventoryTransaction.transactionDate, usageQuantity = usageMultiplier * itemCount.CycleQuantity }; _context.InventoryTransactions.Add(addInventoryTransaction); _context.SaveChanges(); }
I would like to make my SaveChanges only once at the end of a large loop.
c # identity entity-framework
Glenn gordon
source share