title: EF in C# child record is deleted whe updating parent record
body: <p>When adding both parent and child records, the child record is ignored, and when updating a parent record, the child record is deleted if it exists.</p>
  <pre><code>   public ItemResponse&lt;bool&gt; SyncUp(List&lt;DAL.Entities.Task&gt; Tasks, Guid UserClientID)
      {
          using (IUnitOfWork uow = new DAL.Data.UnitOfWork(new DAL.DataContext.DatabaseContext(DAL.DataContext.DatabaseContext.ops.dbOptions)))
          {
              var Res = AddUpdateRange(uow, Tasks);
              uow.Complete();
              return Res;
          }
      }
  </code></pre>
  <p>Then calling this</p>
  <pre><code> public ItemResponse&lt;bool&gt; AddUpdateRange(Web.DAL.Inteface.IUnitOfWork uow, List&lt;DAL.Entities.Task&gt; tasks)
      {
          try
          {
              var taskOrganisationRels = tasks.SelectMany(p =&gt; p.OrganisationTaskRels).DistinctBy(p=&gt;p.ID).ToList();
              var taskStoreRels = tasks.SelectMany(p =&gt; p.StoreTaskRels).DistinctBy(p=&gt;p.ID).ToList();
              var taskUserCompletionRels = tasks.SelectMany(p =&gt; p.TaskUserCompletionRels).DistinctBy(p=&gt;p.ID).ToList();
              var taskJobTitleRels = tasks.SelectMany(p =&gt; p.JobTitleTaskRels).DistinctBy(p=&gt;p.ID).ToList();
              var tasksNotesRels = tasks.SelectMany(p =&gt; p.TaskNoteRels).DistinctBy(p=&gt;p.ID).ToList();
              var tasksUsersRels = tasks.SelectMany(p =&gt; p.TaskUserRels).DistinctBy(p=&gt;p.ID).ToList();
              var notes = tasksNotesRels.Select(p =&gt; p.Note).DistinctBy(p=&gt;p.ID).ToList();
  
  
              var tasksList = tasks.Select(p =&gt;
              {
                  p.TaskNoteRels.Clear();
                  p.CaseTaskRels.Clear();
                  p.TaskUserRels.Clear();
                  p.JobTitleTaskRels.Clear();
                  p.OrganisationTaskRels.Clear();
                  p.StoreTaskRels.Clear();
                  p.LastSyncDateTime = DateTimeOffset.Now; 
                  return p;
              }).DistinctBy(p =&gt; p.ID).ToList();
              uow.Tasks.AddUpdateRange(tasksList);
  
              if (tasksNotesRels.Any())
              {
                  if (notes.Any())
                  {
                      (new NoteLogic()).AddUpdateRange(uow, notes);
                  }
  
                  uow.TaskNoteRel.AddUpdateRange(tasksNotesRels.Select(p =&gt;
                  {
                      p.Note = null;
                      p.Task = null;
                      p.LastSyncDateTime = DateTimeOffset.Now;
                      return p;
                  }).DistinctBy(p =&gt; p.ID));
              }
  
              return new ItemResponse&lt;bool&gt;(true);
          }
          catch (Exception Exc)
          {
              throw Exc;
          }
      }
  </code></pre>
  <p>Then this where this is where it's breaking</p>
  <pre><code>public ItemResponse&lt;bool&gt; AddUpdateRange(Web.DAL.Inteface.IUnitOfWork uow, List&lt;Entity.Note&gt; Items)
      {
  
          try
          {
              var NoteFileRels = Items.Where(p =&gt; p.NoteFileRels.Any()).SelectMany(p =&gt; p.NoteFileRels).ToList();
              var Files = NoteFileRels.Where(p =&gt; p.File != null).Select(p =&gt; p.File).ToList();
  
              uow.Notes.AddUpdateRange(Items);
              if (NoteFileRels.Any())
              {
                  if (Files.Any())
                  {
                      (new FileLogic()).AddUpdateRange(uow, Files);
                  }
                  uow.NoteFileRel.AddUpdateRange(NoteFileRels.Select(p=&gt;
                  {
                      p.Note = null;
                      p.File = null;
                      return p;
                  }).DistinctBy(p =&gt; p.ID));
              }
              return new ItemResponse&lt;bool&gt;(true, null);
          }
          catch (Exception Exc)
          {
              return new ItemResponse&lt;bool&gt;(false, Exc);
          }
      }
  </code></pre>
  <p>Here to be be specific, this is insert is ignored if the record does not exists, and the update of the  uow.Notes.AddUpdateRange(Items) or uow.Notes.AddUpdateRange(Items) trigers a delete if the child record already exists</p>
  <pre><code>  uow.NoteFileRel.AddUpdateRange(NoteFileRels.Select(p=&gt;
              {
                  p.Note = null;
                  p.File = null;
                  return p;
              }).DistinctBy(p =&gt; p.ID));
  </code></pre>