Can Entity Framework add many related objects with one SaveChanges ()?

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.

+7
c # identity entity-framework
source share
3 answers

You do not need to save changes every time if you use objects that refer to newly created objects, and not to identifiers:

 var addItemTracking = new ItemTracking { ... } _context.ItemTrackings.Add(addItemTracking); var addInventoryTransaction = new InventoryTransaction { itemTracking = addItemTracking, ... }; _context.InventoryTransactions.Add(addInventoryTransaction); ... _context.SaveChanges(); 
+13
source share

Since they are all new, not

 itemTrackingID = addItemTracking.ItemTrackingID, 

you can go with

 addItemTracking.InventoryTransaction = addInventoryTransaction; 

(or any other navigation property associated with it) and completely pull _context.SaveChanges () from the loop. Entity Framework is very good at inserting object graphs when everything is new. When saving graphs of objects containing both new and existing elements, the associated identifier is always safer.

+3
source share

What about:

 var trackingItems = itemCounts .Select(i => new ItemTracking { availabilityStatusID = availabilityStatusId, itemBatchId = i.ItemBatchId, locationID = locationId, serialNumber = serialNumber, trackingQuantityOnHand = i.CycleQuantity }); _context.ItemTrackings.AddRange(trackingItems); _context.SaveChanges(); var inventoryTransactions = trackingItems .Select(t => new InventoryTransaction { activityHistoryID = newInventoryTransaction.activityHistoryID, itemTrackingID = t.ItemTrackingID, personID = newInventoryTransaction.personID, usageTransactionTypeId = newInventoryTransaction.usageTransactionTypeId, transactionDate = newInventoryTransaction.transactionDate, usageQuantity = usageMultiplier * t.trackingQuantityOnHand }); _context.InventoryTransactions.AddRange(inventoryTransactions); _context.SaveChanges(); 

However, I have not worked with EF for quite some time, and the code above is written in notepad, so I can not vouch for it

0
source share

All Articles