简体   繁体   English

实体框架可以使用单个SaveChanges()添加许多相关实体吗?

[英]Can Entity Framework add many related entities with single SaveChanges()?

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. 我正在向数据库编写许多(20多个)父子数据集,并且EF要求我在每个集合之间保存更改,否则它会抱怨无法找出主键。 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? 是否可以将数据刷新到SQL Server,以便EF可以从身份中获取主键,并在写入所有更改结束时发送SaveChanges?

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. 我想在大循环结束时只做一次我的SaveChanges。

You don`t need to save changes every time if you use objects refernces to newly created objects not IDs: 如果您使用对象引用新创建的对象而不是ID,则不需要每次都保存更改:

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

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. (或任何相关的导航属性)并将_context.SaveChanges()完全拉出循环。 Entity Framework is very good at inserting object graphs when everything is new. 当一切都是新的时,Entity Framework非常擅长插入对象图。 When saving object graphs containing both new and existing items setting the associated id is always safer. 保存包含新项目和现有项目的对象图表时,设置关联的ID始终更安全。

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 但是我已经有很长一段时间没用EF了,上面的代码是用记事本写的,所以我不能保证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM