title: Duplicating C# ASP.net object recursively with child & parent nodes
body: <p>I am trying to create an endpoint to take the ID of the "manualMaster" and duplicate it &amp; all of its children which reside inside of a tree like structure, each node has a parent which are all tied to the main parent being the ManualMaster. </p>
  
  <p>Below are the models used</p>
  
  <pre><code>public class ManualStep
  {
      public int Id { get; set; }
      public string Title { get; set; }
      public string Description { get; set; }
      public string Directions { get; set; }
      public int MasterId { get; set; }
      public ManualMaster Master { get; set; }
      public int Index { get; set; } // This property governs where inside of the manual master this step belongs
      public virtual ICollection&lt;Image&gt; Images { get; set; } // Virtual because I think I'd always like to have the attached image objects.
      public DateTime Created { get; set; }
      public DateTime LastModified { get; set; }
  
      //Begin tree storage scheme
      public int? ParentId { get; set; }
      public ManualStep Parent { get; set; }
      public ICollection&lt;ManualStep&gt; Nodes { get; set; }
  }
  
  public class ManualMaster
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public int BrandId { get; set; }
      public Brand Brand { get; set; }
      public string Comment { get; set; }
      public ICollection&lt;ManualStep&gt; Steps { get; set; } 
      public DateTime Created { get; set; }
      public DateTime LastModified { get; set; }
  }
  </code></pre>
  
  <p>And then the endpoint which takes the ID and attempts to duplicate.</p>
  
  <pre><code>// GET/DUPLICATE: api/Manual/5
      [Route("{id}")]
      [HttpGet]
      [ResponseType(typeof(ManualMaster))]
      public async Task&lt;IHttpActionResult&gt; DuplicateManualMaster(int id)
      {
          ManualMaster manualMaster = await db.ManualMasters
              .Include(i =&gt; i.Steps)
              .FirstOrDefaultAsync(i =&gt; i.Id == id);
          //
          if (manualMaster == null)
          {
              return NotFound();
          }
  
          var dup = new ManualMaster
          {
              Brand = db.Brands.FirstOrDefault(i =&gt; i.Id == manualMaster.BrandId),
              BrandId = manualMaster.BrandId,
              //manualMaster.Id,
              Name = manualMaster.Name,
              Comment = manualMaster.Comment                
          };
  
          foreach (var step in manualMaster.Steps)
          {
              var newStep = step;
              newStep.Parent.Id = dup.Id;
              db.ManualSteps.Add(newStep);
              await db.SaveChangesAsync();
          }
  
          db.ManualMasters.Add(dup);
          await db.SaveChangesAsync();
  
          return Ok(dup);
  
      }
  </code></pre>
  
  <p>My problem is that currently when duplicating, I am setting the id of every node to the main parent ID and not to their respective ID's which create the tree structure.</p>
  
  <pre><code> newStep.Parent.Id = dup.Id;
  </code></pre>
  
  <p>So instead of copying the entire tree all of my nodes have the same ID... Instead I need to copy the tree structure how it currently is...</p>