title: Incomplete EF code-first cascade delete on many to many relationship
body: <p>I have a <code>PhoneNumber</code> entity which I'd like to reference across multiple entities. For example, a <code>Contact</code> entity that <em>has many</em> PhoneNumbers, and a <code>Business</code> entity with <em>one</em> PhoneNumber.</p>
  
  <pre><code>public class PhoneNumber
  {
      [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      public string Number { get; set; }
  }
  
  public class Contact
  {
      [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public virtual ICollection&lt;PhoneNumber&gt; PhoneNumbers { get; set; }
  }
  
  public class Business
  {
      [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [ForeignKey("PhoneNumber")]
      public int PhoneNumberId { get; set; }
      public virtual PhoneNumber PhoneNumber { get; set; }        
  }
  </code></pre>
  
  <p>I've setup <code>Contact</code> and <code>Business</code> so that they have one way navigation properties. Also, the phone numbers are optional. I've also setup the many relationship for <code>Contact</code> to prevent EF from adding a <code>Contact_Id</code> column when I add a migration. The mapping is as follows (note <code>WithMany()</code> is used since <code>PhoneNumber</code> doesn't have a nav property back to <code>Contact</code>):</p>
  
  <pre><code>modelBuilder.Entity&lt;Contact&gt;().HasMany(r =&gt; r.PhoneNumbers).WithMany()
              .Map(x =&gt; x.MapLeftKey("ContactId").MapRightKey("PhoneId"));
  </code></pre>
  
  <p>When I add a <code>Contact</code> with multiple phone numbers it gets added fine. There are records for the <code>Contact</code>, the <code>PhoneNumbers</code> table, and the <code>ContactPhoneNumbers</code> link table.</p>
  
  <p>However, the issue I'm struggling with is when I delete a contact. EF correctly deletes the entry in the <code>ContactPhoneNumbers</code> link table, and the <code>Contact</code> entry, but it doesn't delete the entries from the <code>PhoneNumbers</code> table. I've seen examples of mapping with <code>modelBuilder</code> where <code>WillCascadeOnDelete(true)</code> is used, but that option isn't available when using <code>WithMany()</code>.</p>
  
  <p>What do I need to do to get that type of cascade delete working correctly? Is it possible with this setup? Or will I need to have a separate <code>PhoneNumbers</code> table for each entity (Contact and Business) to setup a relationship where the respective PhoneNumber table uses a FK (eg., <code>Contact_Id</code>)?</p>
  
  <p>I'm fairly new to EF so any suggestions are welcome. I might be going about this entirely wrong.</p>
  
  <p>EDIT: here's the related migration code...</p>
  
  <pre><code>CreateTable(
      "dbo.PhoneNumbers",
      c =&gt; new
          {
              Id = c.Int(nullable: false, identity: true),
              Number = c.String()
          })
      .PrimaryKey(t =&gt; t.Id);
  
  CreateTable(
      "dbo.Contacts",
      c =&gt; new
          {
              Id = c.Int(nullable: false, identity: true)
          })
      .PrimaryKey(t =&gt; t.Id);
  
  CreateTable(
      "dbo.ContactPhoneNumbers",
      c =&gt; new
          {
              ContactId = c.Int(nullable: false),
              PhoneId = c.Int(nullable: false),
          })
      .PrimaryKey(t =&gt; new { t.ContactId, t.PhoneId })
      .ForeignKey("dbo.Contacts", t =&gt; t.ContactId, cascadeDelete: true)
      .ForeignKey("dbo.PhoneNumbers", t =&gt; t.PhoneId, cascadeDelete: true)
      .Index(t =&gt; t.ContactId)
      .Index(t =&gt; t.PhoneId);
  
  CreateTable(
      "dbo.Business",
      c =&gt; new
          {
              Id = c.Int(nullable: false),
              PhoneNumberId = c.Int(nullable: false)
          })
      .PrimaryKey(t =&gt; t.Id)
      .ForeignKey("dbo.PhoneNumbers", t =&gt; t.PhoneNumberId, cascadeDelete: true)
      .Index(t =&gt; t.Id)
      .Index(t =&gt; t.PhoneNumberId);
  </code></pre>