title: EF Core Bulk Extensions: Index was out of range on BulkInsert
body: <p>I am getting the following error while using bulk insert from EF core bulk extensions:</p>
  
  <blockquote>
    <p>Index was out of range. Must be non-negative and less than the size of
    the collection. (Parameter 'index')    at
    System.Collections.Generic.List<code>1.get_Item(Int32 index)    at
    EFCore.BulkExtensions.TableInfo.UpdateEntitiesIdentity[T](IList</code>1
    entities, IList<code>1 entitiesWithOutputIdentity)    at
    EFCore.BulkExtensions.TableInfo.LoadOutputData[T](DbContext context,
    IList</code>1 entities)    at
    EFCore.BulkExtensions.SqlBulkOperation.Merge[T](DbContext context,
    IList<code>1 entities, TableInfo tableInfo, OperationType operationType,
    Action</code>1 progress)    at
    EFCore.BulkExtensions.DbContextBulkExtensions.BulkInsert[T](DbContext
    context, IList<code>1 entities, BulkConfig bulkConfig, Action</code>1 progress)<br>
    at MyProject.Data.Repository.Repository<code>1.CreateRange(List</code>1 entities)</p>
  </blockquote>
  
  <p>I have a parent entity and child entity. What I am trying to do is, first bulk insert parent and then assign Ids generated for parent to their children. Then bulk insert children.</p>
  
  <p><code>CreateRange</code> method:</p>
  
  <pre><code>public List&lt;T&gt; CreateRange(List&lt;T&gt; entities)
  {
      var bulkConfig = new BulkConfig { PreserveInsertOrder = true, SetOutputIdentity = true };
      _dbContext.BulkInsert(entities, bulkConfig);
      return entities;
  }
  </code></pre>
  
  <p>Bulk insert code:</p>
  
  <pre><code>// Index parents. (Since PreserveInsertOrder = true is set.)
  parents.ForEach(x =&gt; x.Id = parents.IndexOf(x) + 1);
  _parentRepository.CreateRange(parents);
  
  // parent-child relation mapping.
  parents.ForEach(x =&gt; x.Children.ToList().ForEach(y =&gt; y.ParentId = x.Id));
  
  var children = parents.SelectMany(x =&gt; x.Children).ToList();
  // Index children. (Since PreserveInsertOrder = true is set.)
  children.ForEach(x =&gt; x.Id = children.IndexOf(x) + 1);
  _childRepository.CreateRange(children);
  </code></pre>
  
  <p>It's not always possible to reproduce this issue. I am not able to identify the condition for reproducing this issue.</p>