BulkInsertOrUpdate 子实体使用 efcore.bulkextensions

BulkInsertOrUpdate with child entity use of efcore.bulkextensions

我正在使用 BulkInsertOrUpdateAsync 方法在 net-core 3.0 中更新插入 20000 条记录。这样做时完全没有错误。它正在将所有包含父实体和子实体的记录更新到数据库中。

var bulkConfig = new BulkConfig()
            {
                SetOutputIdentity = true,
                PreserveInsertOrder = true
            };   
var subEntities = new List<ItemHistory>(); 
await _dbContext.BulkInsertOrUpdateAsync(entities, bulkConfig);
    
foreach (var entity in entities)
{
   foreach (var subEntity in entity.ItemHistories)
    {
     subEntity.ItemId = entity.ID; // setting FK to match its linked PK that was generated in DB
    }
    subEntities.AddRange(entity.ItemHistories);
}
await _airportDBContext.BulkInsertOrUpdateAsync(subEntities})

但是当我将记录检入数据库时​​,许多具有子实体的记录都引用了错误 'ItemId'。即使当我再次使用现有记录执行此操作时,它也会将一些记录再次插入到子实体中。而对于父实体来说,在这两种情况下都可以正常工作。我的代码有什么问题吗?还是此软件包的已知问题?

我指的是这个https://github.com/borisdj/EFCore.BulkExtensions#read-example

它可能与这个问题相同:- https://www.bountysource.com/issues/76836788-bulkinsertorupdateasync-ids-not-setting-correctly

有没有人知道如何解决这个问题。

这是对此的回答

var bulkConfig = new BulkConfig()
            {
                SetOutputIdentity = true,
                PreserveInsertOrder = true
            };   
var subEntities = new List<ItemHistory>(); 
entities = entities.ForEach(i => i.ID == 0);
await _dbContext.BulkInsertOrUpdateAsync(entities, bulkConfig);

    
foreach (var entity in entities)
{
   foreach (var subEntity in entity.ItemHistories)
    {
     subEntity.ItemId = entity.ID; // setting FK to match its linked PK that was generated in DB
    }
    subEntities.AddRange(entity.ItemHistories);
}
await _airportDBContext.BulkInsertOrUpdateAsync(subEntities});