title: EF Doesn't Delete Records For Fluent API - Many To Many Relationship
body: <p>I have 2 entities, </p>
  
  <ul>
  <li>News</li>
  <li>FileAttachment</li>
  </ul>
  
  <p>I wanted to configure using code-first fluent API so that Each News can have 0,1 or more than 1 attachments.</p>
  
  <p>here is what i'm using right now</p>
  
  <pre><code>   public NewsMap()
      {
          this.ToTable("News"); // Table Name
          this.HasKey(m =&gt; m.Id); // Primary Key
  
          // Field Definition            
          this.Property(m =&gt; m.Title).HasMaxLength(255).IsRequired();
          this.Property(m =&gt; m.Body).HasColumnType("Text").IsRequired();
          this.Property(m =&gt; m.Summary).HasMaxLength(1000).IsRequired();
          this.Property(m =&gt; m.AuthorId).IsRequired();
  
          this.Property(m =&gt; m.CreatedOn).IsRequired();
          this.Property(m =&gt; m.UpdatedOn).IsRequired();
  
          this.HasMany(m =&gt; m.Attachments).WithMany().Map(m =&gt; m.MapLeftKey("NewsId").MapRightKey("AttachmentId"));
      }
  
  public class FileAttachmentMap : EntityTypeConfiguration&lt;FileAttachment&gt;
  {
      public FileAttachmentMap()
      {
          this.ToTable("FileAttachments"); // Table Name
          this.HasKey(m =&gt; m.Id); // Primary Key
  
          // Field Definition            
          this.Property(m =&gt; m.DisplayName).HasMaxLength(256).IsRequired();
          this.Property(m =&gt; m.PhysicalFileName).HasMaxLength(256).IsRequired();
          this.Property(m =&gt; m.Extension).HasMaxLength(50).IsRequired();
          this.Property(m =&gt; m.IsImage).IsRequired();
          this.Property(m =&gt; m.ThumbTiny).HasMaxLength(275).IsOptional();
          this.Property(m =&gt; m.ThumbSmall).HasMaxLength(275).IsOptional();
          this.Property(m =&gt; m.ThumbMid).HasMaxLength(275).IsOptional();
          this.Property(m =&gt; m.ByteSize).IsRequired();
          this.Property(m =&gt; m.StorageType).IsRequired();   
  
          this.Property(m =&gt; m.CreatedOn).IsRequired();
          this.Property(m =&gt; m.UpdatedOn).IsRequired();
      }
  }
  </code></pre>
  
  <p>This mapping correctly generates an intermediate table named  NewsFileAttachment with two fields : </p>
  
  <ul>
  <li>NewsId</li>
  <li>AttachmentId</li>
  </ul>
  
  <p>On News Entity when i call News.Attachments.Add(Attachment); it correctly adds records in both Attachment &amp; NewsAttachment tables.</p>
  
  <p>When i remove some list item from News.Attachments it correctly removes record from NewsAttachment table, but it doesn't delete record in FileAttachment table. I wanted to remove that too. </p>
  
  <p>Can someone please suggest a better Fluent API configuration to achieve this?</p>
  
  <p>Thanks,
  Amit</p>
  
  <p><strong>EDIT</strong><br/><br/>
  In my case FileAttachment stores files for various purpose. i've Blog entity that too have attachments. So, two intermediate tables BlogAttachments &amp; FileAttachments. Now if i use WithOptional as (I can't use WithRequired as i need BlogId &amp; NewsId both in FileAttachment table), i can get rid off intermediate table, but still delete doesn't delete record from FileAttachment table, it just make NewsId/BlogId NULL. </p>
  
  <p>Any suggestion? Main thing is I do not wanted to create separate tables with all the fields i have in FileAttachment table.</p>