c# IEnumerable Update object I have this code that I am using to update OriginalId using the Id value. I there a better way to do this then using all the loops?

Controller

TemplatesDataService.UpdateRegimenCodeId(template.Regimens);


Interface 

void UpdateDosageInstructionsCodeId(IEnumerable&lt;Regimen&gt; Regimens);


DataService 

public void UpdateDosageInstructionsCodeId(IEnumerable&lt;Regimen&gt; regimens)
        {
            if (regimens != null)
            {
                foreach (var regimen in regimens)
                {
                    Debug.Write(string.Format("{0} {1}", "regimenId", regimen.Id));
                    regimen.OriginalId = regimen.Id;
                    foreach (var regimenPart in regimen.RegimenParts)
                    {
                        regimenPart.OriginalId = regimenPart.Id;
                        Debug.Write(string.Format("{0} {1}", "regimenPart", regimenPart.Id));
                        foreach (var entries in regimenPart.RegimenEntries)
                        {
                            entries.OriginalId = entries.Id;
                            Debug.Write(string.Format("{0} {1}", "RegimenEntries", entries.Id));
                            foreach (var dosage in entries.DosageInstructions)
                            {
                                Debug.Write(string.Format("{0} {1}", "DosageInstructions", dosage.Id));
                                dosage.OriginalId = dosage.Id;
                                UnitOfWork.Save();
                            }
                        }
                    }
                }
            }
        }
I like the suggestions to let SQL do the foreign key cascading for you. Also, have you considered generating the id's outside the system (e.g. Guid.NewGuid()) and pushing them in?

Whatever the constraints and limitations you're working under, here's my take on a class hierarchy to handle your use case (via inheritance rather than encapsulation). 

While it might be a bit over-designed for just handling Id's, the idea here is to create an object model upon which you can expand the application to arbitrary size.  The example also aims to minimize code repetition.

using System;
using System.Collections.Generic;
using System.Diagnostics;

class App_DosageSchedule
{
    public void Run()
    {
        var regimens = new List&lt;Regimen&gt;();
        var schedule = new Schedule(regimens);
        schedule.PreserveIds();
        schedule.Save();
    }
}   

public abstract class Component
{
    public Guid Id { get; protected set; }
    public Guid OriginaId { get; protected set; }
    public void PreserveId()
    {
        OriginaId = Id;
        Debug.Write($"{GetType().Name} {Id}");
    }
}

public abstract class ComponentWithChildren : Component
{
    public void PreserveIds()
    {
        PreserveId();
        preserveChildIds();
    }

    protected abstract void preserveChildIds();
}

public class Schedule
{
    public List&lt;Regimen&gt; Regimens { get; private set; }
    public Schedule(List&lt;Regimen&gt; regimens) =&gt; Regimens = regimens;

    public void PreserveIds() =&gt; Regimens.ForEach(r =&gt; r.PreserveIds());

    public void Save()
    {
        ///write to SQL
    }
}

public class Regimen: ComponentWithChildren
{               
    public List&lt;RegimenPart&gt; Parts { get; private set; }
    protected override void preserveChildIds() =&gt; Parts.ForEach(p =&gt; p.PreserveIds());
}

public class RegimenPart : ComponentWithChildren
{       
    public List&lt;DosageInstruction&gt; Dosages{ get; private set; }
    protected override void preserveChildIds() =&gt; Dosages.ForEach(d =&gt; d.PreserveId());
}

public class DosageInstruction : Component
{       
}