EF Core with many to many circular reference relationship inhibiting delete
<p>I have been struggling with the following scenario, while using EF Core:</p>

<p>A particular entity, let's call it Person, has a many to many relationship with itself [an instance of Person may contain a list of other Person instances]. 
The join entity embodying this many to many relationship, call it PersonPerson, has been configured with OnDelete(DeleteBehaviour.Restrict), 
in order to break the circular reference/cascade delete.  But this seems to be hindering the normal deletion of these PersonPerson instances.</p>

<p>The error is</p>

<blockquote>
  <p>InvalidOperationException: The association between entity types 'Person' and 'PersonPerson' has been severed but the foreign key for this 
  relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.</p>
</blockquote>

<p>Is this a catch 22 situation, or am I not deleting correctly?</p>



<pre class="lang-cs prettyprint-override"><code>public class Person
{
        public int Id { get; set; }

        -some person properties-

        public ICollection&lt;PersonPerson&gt; Parents { get; set; }
        public ICollection&lt;PersonPerson&gt; Children { get; set; }

}

public class PersonPerson
{
        [Required]
        public int PersonParentId { get; set; }
        [Required]
        public int PersonChildId { get; set; }

        public Person Parent { get; set; }
        public Person Child { get; set; }
}

protected override void OnModelCreating(ModelBuilder builder)
{
            base.OnModelCreating(builder);
...
            builder.Entity&lt;PersonPerson&gt;()
                .HasKey(i =&gt; new { i.PersonParentId, i.PersonChildId });

            builder.Entity&lt;PersonPerson&gt;()
                .HasOne(i =&gt; i.Parent)
                .WithMany(i =&gt; i.Children)
                .HasForeignKey(i =&gt; i.ParentParentId)
                .OnDelete(DeleteBehavior.Restrict);

            builder.Entity&lt;PersonPerson&gt;()
                .HasOne(i =&gt; i.Child)
                .WithMany(i =&gt; i.Parents)
                .HasForeignKey(i =&gt; i.PersonChildId)
                .OnDelete(DeleteBehavior.Restrict);
...
</code></pre>

<p>In the controller, there is:</p>

<pre class="lang-cs prettyprint-override"><code>Person person;
PersonPerson instanceToRemove;
...
person.Children.Remove(instanceToRemove);
...
_context.Update(person);
await _context.SaveChangesAsync();
</code></pre>