【发布时间】:2018-12-03 04:03:03
【问题描述】:
更新列表中的多条记录以加快处理速度的最佳方法是什么?
目前,我正在更新大约 15000 种产品,每种产品都有 3 个不同的价格集,需要一整天才能完成。
我需要在代码端一次更新所有价格,然后一次性将这些更改提交到数据库,而不是获取每个库存项目,更新其值,然后将其附加到上下文中。每次提取都会导致延迟。
代码
public void UpdatePricesFromInventoryList(IList<Domain.Tables.Inventory> invList)
{
var db = new UniStockContext();
foreach (var inventory in invList)
{
Domain.Tables.Inventory _inventory = db.Inventories
.Where(x => x.InventoryID == inventory.InventoryID)
.FirstOrDefault();
if (inventory.Cost.HasValue)
_inventory.Cost = inventory.Cost.Value;
else
_inventory.Cost = 0;
foreach (var inventoryPrices in inventory.AccInventoryPrices)
{
foreach (var _inventoryPrices in _inventory.AccInventoryPrices)
{
if (_inventoryPrices.AccInventoryPriceID == inventoryPrices.AccInventoryPriceID)
{
_inventoryPrices.ApplyDiscount = inventoryPrices.ApplyDiscount;
_inventoryPrices.ApplyMarkup = inventoryPrices.ApplyMarkup;
if (inventoryPrices.Price.HasValue)
_inventoryPrices.Price = inventoryPrices.Price.Value;
else
_inventoryPrices.Price = _inventory.Cost;
if (inventoryPrices.OldPrice.HasValue)
{
_inventoryPrices.OldPrice = inventoryPrices.OldPrice;
}
}
}
}
db.Inventories.Attach(_inventory);
db.Entry(_inventory).State = System.Data.Entity.EntityState.Modified;
}
db.SaveChanges();
db.Dispose();
}
我还尝试根据此 SOQ Entity Framework update/insert multiple entities 处理我的代码 它给了我和错误。以下是详细信息:
代码:
public void UpdatePricesFromInventoryListBulk(IList<Domain.Tables.Inventory> invList)
{
var accounts = new List<Domain.Tables.Inventory>();
var db = new UniStockContext();
db.Configuration.AutoDetectChangesEnabled = false;
foreach (var inventory in invList)
{
accounts.Add(inventory);
if (accounts.Count % 1000 == 0)
{
db.Set<Domain.Tables.Inventory>().AddRange(accounts);
accounts = new List<Domain.Tables.Inventory>();
db.ChangeTracker.DetectChanges();
db.SaveChanges();
db.Dispose();
db = new UniStockContext();
}
}
db.Set<Domain.Tables.Inventory>().AddRange(accounts);
db.ChangeTracker.DetectChanges();
db.SaveChanges();
db.Dispose();
}
错误:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
【问题讨论】:
-
感谢您为我指明正确的方向。我现在正在尝试该代码。它之前没有出现在我的搜索中:/
-
@CodeNotFound 解决方案不完整。我现在试了一下,它给了我一个错误。也将使用该代码试用更新我的问题。
标签: c# entity-framework-6