title: How to delete Many - Many Relationship (Only Joining Table)
body: <p>I'm using Entity Framework Core in an ASP.NET Core application. I want to be able to delete the joining linking row in a many to many relationship table (where you only have table1Id &amp; table2Id columns linking the many to many relationship) but the only options I have are </p>
  
  <p>1) SetNull (which I'm assuming tries to set table2Id column ON TABLE2 to null because I can't not perform a update-database and it tells me the reason is because it can't set the foreign key id column to null)</p>
  
  <p>2) Restrict (this is a band-aid fix because it allows me to push but now my users can't delete until they've undone every relationship before deleting)</p>
  
  <p>3) Cascade (this tries to delete EVERYTHING and is definitely not what I want)</p>
  
  <p>there doesn't seem to be just a "DeleteRelationship" option</p>
  
  <p>I have tried to set the id properties in the linking class to nullable (see code) </p>
  
  <pre><code>public class PregenProfessionArchetypeModel
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public ICollection&lt;PregenProfessionArchetype_ParentSkill_LinkModel&gt; TrainedParentSkills { get; set; } = new List&lt;PregenProfessionArchetype_ParentSkill_LinkModel&gt;();
      public ICollection&lt;PregenProfessionArchetype_ChildSkill_LinkModel&gt; ChildSkills { get; set; } = new List&lt;PregenProfessionArchetype_ChildSkill_LinkModel&gt;();
      public ICollection&lt;PregenProfessionArchetype_Technique_LinkModel&gt; Techniques { get; set; } = new List&lt;PregenProfessionArchetype_Technique_LinkModel&gt;();
      public ICollection&lt;PregenProfessionArchetype_Talent_LinkModel&gt; Talents { get; set; } = new List&lt;PregenProfessionArchetype_Talent_LinkModel&gt;();
  }
  
  
  public class PregenProfessionArchetype_ChildSkill_LinkModel
  {
      public int PregenProfessionArchetypeId { get; set; }
      public PregenProfessionArchetypeModel PregenProfessionArchetype { get; set; }
      public int ChildSkillId { get; set; }
      public ChildSkillModel ChildSkill { get; set; }
  }
  
  public class PregenProfessionArchetype_ParentSkill_LinkModel
  {
      public int? PregenProfessionArchetypeId { get; set; }
      public PregenProfessionArchetypeModel PregenProfessionArchetype { get; set; }
      public int? ParentSkillId { get; set; }
      public ParentSkillModel ParentSkill { get; set; }
  }
  
  public class ChildSkillModel
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public int ParentSkillId { get; set; }
      [Display(Name = "Parent Skill")]
      public ParentSkillModel ParentSkill { get; set; }
      public int Cost { get; set; } = 1;
  
  
      public ICollection&lt;PregenProfessionArchetype_ChildSkill_LinkModel&gt; PregenProfessionArchetypeLink { get; set; } = new List&lt;PregenProfessionArchetype_ChildSkill_LinkModel&gt;();
  }
  
  
          // Application Context File Overwritting (Child is same and left out talent)
          builder.Entity&lt;PregenProfessionArchetype_ParentSkill_LinkModel&gt;()
              .HasKey(ttp =&gt; new { ttp.ParentSkillId, ttp.PregenProfessionArchetypeId });
  
          builder.Entity&lt;PregenProfessionArchetype_ParentSkill_LinkModel&gt;()
              .HasOne&lt;PregenProfessionArchetypeModel&gt;(tm =&gt; tm.PregenProfessionArchetype)
              .WithMany(tpp =&gt; tpp.TrainedParentSkills)
              .HasForeignKey(tm =&gt; tm.PregenProfessionArchetypeId)
              .OnDelete(DeleteBehavior.Restrict);
  
          builder.Entity&lt;PregenProfessionArchetype_ParentSkill_LinkModel&gt;()
              .HasOne&lt;ParentSkillModel&gt;(tm =&gt; tm.ParentSkill)
              .WithMany(tmm =&gt; tmm.PregenProfessionArchetypeLink)
              .HasForeignKey(tm =&gt; tm.PregenProfessionArchetypeId)
              .OnDelete(DeleteBehavior.Restrict);
  </code></pre>
  
  <p>The only time I'm able to push is when it is set to restricted. The code shows an example of the classes and their relationship</p>
  
  <p>Error Message: SetNull</p>
  
  <pre><code>Failed executing DbCommand (81ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  ALTER TABLE [PregenProfessionArchetype_ParentSkill_LinkModel] ADD CONSTRAINT [FK_PregenProfessionArchetype_ParentSkill_LinkModel_ParentSkillModel_PregenProfessionArchetypeId] FOREIGN KEY ([PregenProfessionArchetypeId]) REFERENCES [ParentSkillModel] ([Id]) ON DELETE SET NULL;
  </code></pre>