title: How to insert duplicated records in Entity Framework
body: <p>I am having very strange situation regarding insertion of duplicated records.
  I am retrieving records against certain criteria and after modifying couple of properties, I am re-inserting the entire collection of objects again without even changing the primary key values of objects.</p>
  
  <p>But I am not getting expected results. My all collections nested inside parent collection never added into the database completely. </p>
  
  <p>I have no idea, what is going wrong. Do I need to detach all these entities completely? 
  I am already retrieving entities with the usage of NoTracking() and even detaching parent entity while modifying it's attributes.</p>
  
  <p>My Parent Collection is Consignment and containing child entities as list "ConsignmentLine"</p>
  
  <p>My Entity heirarchy is :</p>
  
  <pre><code>public class Consignment 
  {
      public int ConsignmentId { get; set; }
      public int ClientSubsidiaryId { get; set; }
      public int ForwarderId { get; set; }
      public int ClientId { get; set; } 
  
      public ICollection&lt;ConsignmentLine&gt; ConsignmentLines { get; set; }    
  
      public Consignment()
      {          
        ConsignmentLines = new List&lt;ConsignmentLine&gt;();            
      }
  }
  
  public class ConsignmentLine
  {
      public int ConsignmentLineId { get; set; }
      public int PackagingId { get; set; }
      public double Amount { get; set; }
      public double Weight { get; set; }                
  
      public int ConsignmentId { get; set; }
      public virtual Consignment Consignment { get; set; }        
  }
  </code></pre>
  
  <p><strong>Steps involved in my code :</strong></p>
  
  <p>Retrieving data :</p>
  
  <pre><code>var Consignments = _dbContext.Consignments.AsNoTracking().Where(Pr =&gt; ( Pr.SourceParty == 0 &amp;&amp; Pr.ParentId == null &amp;&amp; Pr.ConnectState&gt;=4 ) ).ToList();
  </code></pre>
  
  <p>Modifying couple of properties.</p>
  
  <pre><code>consignments.ForEach(
              (consignment) =&gt;
              {
                  consignment.ClientId = clientId;                    
                  _dbContext.Entry(consignment).State = System.Data.Entity.EntityState.Detached;
                  consignment.ForwarderId = forwarderId;
                  consignment.ClientSubsidiaryId = clientSubsidiaryId;                    
              }); 
  </code></pre>
  
  <p>Trying to save in chunks because I know, Consignments having > 250000 records.</p>
  
  <pre><code>const int BulkSize = 1000;
  var SkipSize = 0;
  
  try
  {                                                                                                                        
     while (SkipSize &lt; consignments.Count)
     {
        ProcessableConsignments =   consignments.Skip(SkipSize).Take(BulkSize).ToList();                                                                                                                                                  _dbContext.Configuration.AutoDetectChangesEnabled = false;     
                                  dbContext.Consignments.AddRange(ProcessableConsignments);
  
           var changedRecords = _dbContext.SaveChanges();
           SkipSize = SkipSize +  BulkSize;                                            
         }                                      
   }
   catch (Exception ex)
   {
      throw;
   }
  </code></pre>
  
  <p>I don't know what I am missing here.
  All I need to re-insert the whole BULK of records containing various other child entities as collection second time  ( in fact N times in loop ) as new BULK.</p>