10

I am writing many (20+) parent child datasets to the database, and EF is requiring me to savechanges between each set, without which it complains about not being able to figure out the primary key. Can the data be flushed to the SQL Server so that EF can get the primary keys back from the identities, with the SaveChanges being sent at the end of writing all of the 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 do my SaveChanges just once at the end of the big loop.

py3r3str
  • 1,879
  • 18
  • 23
Glenn Gordon
  • 1,266
  • 1
  • 14
  • 24

3 Answers3

15

You don`t need to save changes every time if you use objects refernces to newly created objects not IDs:

var addItemTracking = new ItemTracking
{
    ...
}
_context.ItemTrackings.Add(addItemTracking);
var addInventoryTransaction = new InventoryTransaction
{
    itemTracking = addItemTracking,
    ...
};
_context.InventoryTransactions.Add(addInventoryTransaction);
...
_context.SaveChanges();
py3r3str
  • 1,879
  • 18
  • 23
3

Since they're all new items rather than

itemTrackingID = addItemTracking.ItemTrackingID,

you could go with

addItemTracking.InventoryTransaction = addInventoryTransaction;

(or whatever the associated navigation property is) and pull the _context.SaveChanges() out of the loop entirely. Entity Framework is very good at inserting object graphs when everything is new. When saving object graphs containing both new and existing items setting the associated id is always safer.

Miniver Cheevy
  • 1,667
  • 2
  • 14
  • 20
0

How 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 haven't worked with EF for quite a while and above code is written in notepad so I cannot vouch for it

Michal Hosala
  • 5,570
  • 1
  • 22
  • 49