0

I am trying to create an endpoint to take the ID of the "manualMaster" and duplicate it & 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.

Below are the models used

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<Image> 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<ManualStep> 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<ManualStep> Steps { get; set; } 
    public DateTime Created { get; set; }
    public DateTime LastModified { get; set; }
}

And then the endpoint which takes the ID and attempts to duplicate.

// GET/DUPLICATE: api/Manual/5
    [Route("{id}")]
    [HttpGet]
    [ResponseType(typeof(ManualMaster))]
    public async Task<IHttpActionResult> DuplicateManualMaster(int id)
    {
        ManualMaster manualMaster = await db.ManualMasters
            .Include(i => i.Steps)
            .FirstOrDefaultAsync(i => i.Id == id);
        //
        if (manualMaster == null)
        {
            return NotFound();
        }

        var dup = new ManualMaster
        {
            Brand = db.Brands.FirstOrDefault(i => 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);

    }

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.

 newStep.Parent.Id = dup.Id;

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...

megazoor
  • 1
  • 3
  • 1
    I think you forgot to ask a question. Read [ask]. – CodeCaster Jun 19 '17 at 17:50
  • `var newStep = step;` is just creating a new reference to the same object, and then you start changing the shared object's properties...you probably want `var newStep = new ManualStep()` – Rufus L Jun 19 '17 at 17:54
  • Possible duplicate of [Deep Copy of a C# Object](https://stackoverflow.com/questions/11074381/deep-copy-of-a-c-sharp-object) – Rufus L Jun 19 '17 at 17:56

0 Answers0