简体   繁体   English

BulkInsertOrUpdate 与子实体使用 efcore.bulkextensions

[英]BulkInsertOrUpdate with child entity use of efcore.bulkextensions

I am using BulkInsertOrUpdateAsync method to upsert the 20000 records in net-core 3.0.我正在使用 BulkInsertOrUpdateAsync 方法在 net-core 3.0 中插入 20000 条记录。 There is no error at all when doing this.这样做时根本没有错误。 it is upserting all the record with parent and child entity into database.它正在将带有父实体和子实体的所有记录更新到数据库中。

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})

But when I am checking the records into database, many of the records having the child entity is referring wrong 'ItemId'.但是当我将记录检查到数据库中时,许多具有子实体的记录都引用了错误的“ItemId”。 Even when I am executing this again with existing record, it is inserting some of the records again into child entity.即使当我使用现有记录再次执行此操作时,它也会将一些记录再次插入到子实体中。 while for the parent entity is working fine in both of the scenario.而对于父实体在这两种情况下都可以正常工作。 Is there any issue in my code?我的代码有问题吗? Or is it a known issue with this package?还是此软件包的已知问题?

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

and it might be same as this issue:- https://www.bountysource.com/issues/76836788-bulkinsertorupdateasync-ids-not-setting-correctly它可能与此问题相同:- https://www.bountysource.com/issues/76836788-bulkinsertorupdateasync-ids-not-setting-correctly

Could you anyone having idea to overcome this problem.你有没有想法克服这个问题。

Here is answer for this这是对此的答案

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});

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

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