title: 'The association between entites has been severed' error when deleting child, with relationship defined in terms of compound key
body: <p>So the preface: I have a pair of models, one that's defined as a 'parent' that has its own ID, properties, and fields for child components:</p>
  <pre class="lang-cs prettyprint-override"><code>public class SearchDesign
  {
      [Key]
      [DataMember]
      public int Id {get; set;}
      
      // ... more properties
      
      [DataMember]
      public virtual ICollection&lt;SearchComponent&gt; SearchComponents { get; set; }
  }
  </code></pre>
  <p>The components themselves are children to the <code>SearchDesign</code>, and are set up to also be representable as a tree.</p>
  <pre class="lang-cs prettyprint-override"><code>public class SearchComponent
  {
      [Key]
      [DataMember]
      public int Id { get; set; }
      [Key]
      [DataMember]
      public int SearchDesignID { get; set; }
      [Required]
      [DataMember]
      public int Order { get; set; }
      [DataMember]
      public int? ParentID { get; set; }
  
      // ... more properties
  
      [DataMember]
      public virtual SearchDesign SearchDesign { get; set; }
      [DataMember]
      public virtual SearchComponent ParentComponent { get; set; }
      [DataMember]
      public virtual ICollection&lt;SearchComponent&gt; ChildComponents { get; set; }
  }
  </code></pre>
  <p>These particular parts of the model are set up in a DBContext like so...</p>
  <pre class="lang-cs prettyprint-override"><code>modelBuilder.Entity&lt;SearchComponent&gt;()
      .HasKey(sc =&gt; new { sc.Id, sc.SearchDesignID });
  
  modelBuilder.Entity&lt;SearchComponent&gt;()
      .HasMany(parent =&gt; parent.ChildComponents)
      .WithOne(child =&gt; child.ParentComponent)
      .HasForeignKey(parent =&gt; new { parent.ParentID, parent.SearchDesignID })
      .IsRequired(false)
      .OnDelete(DeleteBehavior.Restrict);
  
  modelBuilder.Entity&lt;SearchComponent&gt;()
      .HasOne(sc =&gt; sc.SearchDesign)
      .WithMany(sd =&gt; sd.SearchComponents)
      .HasForeignKey(sc =&gt; sc.SearchDesignID)
      .IsRequired(false);         
  </code></pre>
  <p>So there is a point where I attempt to perform an Update function through an API, which calls a function that will overwrite traits on the existing entity on our DB. In the process of doing this we 'remove' the dependent components from the design, to then later re-add them in the state that's reflected in the input that comes from the request:</p>
  <pre class="lang-cs prettyprint-override"><code>//Update case.
  design = design.Clone(); //function that un-links the entity from EF core by using Newtonsoft to de-serialize and then re-serialize it.
  
  SearchDesign existing = LoadDesign(design.Id);
  if (existing == null)
  {
      throw new ArgumentOutOfRangeException();
  }
  else
  {
      existing.Name = design.Name;
      
      ...
  
      //Remove and re-add.
      DBContext.SearchComponents.RemoveRange(existing.SearchComponents); //THIS throws the error.
      foreach (var component in design.SearchComponents)
      {
          component.SearchDesign = existing;
      }
      DBContext.SearchComponents.AddRange(design.SearchComponents);
  }               
  </code></pre>
  <p>Thing is, when I attempt the <code>RemoveRange</code>, which is (I reiterate) attempting to <strong>remove dependent elements</strong>, it throws this error:</p>
  <blockquote>
  <p>System.InvalidOperationException : The association between entities
  'SearchDesign' and 'SearchComponent' with the key value
  '{SearchDesignID: 7}' has been severed but the relationship is either
  marked as 'Required' or is implicitly required because the foreign key
  is not nullable. If the dependent/child entity should be deleted when
  a required relationship is severed, then setup the relationship to use
  cascade deletes.</p>
  </blockquote>
  <p>My instincts lead me to believe this might be something to do with the parent/child loop on the <code>SearchComponent</code> structure, but the error seems to indicate it's an issue pertaining to the design/component link? If that's really the case then this error makes no sense because I'm attempting to remove the dependent/child entity, not the parent, and I can't exactly make the <code>SearchDesignID</code> get set to null, because that's part of the element's key.</p>
  <p>Any advice from EF Core experts would be appreciated, thank you!</p>