title: Foreign Key Violation when saving with a Many To Many relationship EF6
body: <p>I have three tables:</p>
  
  <pre><code>CREATE TABLE [dbo].[Committees] (
      [committee_id]          INT             IDENTITY (1, 1) NOT NULL,
      [Committee_name]        NVARCHAR (128)  NULL,
      [Committee_email]       NVARCHAR (128)  NULL,
      [Committee_inactive]    INT             NULL,
      [Committee_type]        NVARCHAR (50)   NULL,
      [Committee_description] NVARCHAR (1024) NULL,
      [Committee_chair_id]    INT             NOT NULL,
      [Committee_sponsor_id]  INT             NOT NULL,
      [Committee_end_date]    DATETIME2 (7)   NULL,
      [bMembershipOpen]       BIT             CONSTRAINT [DF_Committees_bMembershipOpen] DEFAULT ((0)) NOT NULL,
      CONSTRAINT [PK_Committees] PRIMARY KEY CLUSTERED ([committee_id] ASC),
      CONSTRAINT [FK_CommitteesChair_ToPersons] FOREIGN KEY ([Committee_chair_id]) REFERENCES [dbo].[Persons] ([Id]),
      CONSTRAINT [FK_CommitteesSponsor_ToPersons] FOREIGN KEY ([Committee_sponsor_id]) REFERENCES [dbo].[Persons] ([Id])
  );
  
  CREATE TABLE [dbo].[Persons] (
      [Id]    INT            IDENTITY (1, 1) NOT NULL,
      [Name]  NVARCHAR (128) NULL,
      [Email] NVARCHAR (128) NULL,
      CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED ([Id] ASC)
  );
  </code></pre>
  
  <p>and </p>
  
  <pre><code>CREATE TABLE [dbo].[CommitteeMembers] (
      [CommitteeId] INT NOT NULL,
      [PersonId]    INT NOT NULL,
      PRIMARY KEY CLUSTERED ([CommitteeId] ASC, [PersonId] ASC),
      CONSTRAINT [FK_CommitteeMembers_ToCommittee] FOREIGN KEY ([CommitteeId]) REFERENCES [dbo].[Committees] ([committee_id]),
      CONSTRAINT [FK_CommitteeMembers_ToPerson] FOREIGN KEY ([PersonId]) REFERENCES [dbo].[Persons] ([Id])
  );
  </code></pre>
  
  <p>I have two classes associated with Committees and Persons</p>
  
  <pre><code> public class Committee {
  
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          [Column("committee_id")]
          [Key]
          public int Id { get; set; }
  
          [Column("Committee_chair_id")]
          [ForeignKey("Chairman")]
          [Required(ErrorMessage = "Committee must have a Chairman")]
          public int ChairmanId { get; set; }
  
          [Column("Committee_sponsor_id")]
          [ForeignKey("Sponsor")]
          [Required(ErrorMessage = "Committee must have a Sponsor")]
          public int SponsorId { get; set; }
  
          [Column("Committee_name")]
          [MinLength(3, ErrorMessage = "Committee Name must be at least 3 characters long")]
          [MaxLength(128, ErrorMessage = "Committee Name cannot be longer than 128 characters")]
          [Required(ErrorMessage = "Committee must have a name")]
          public string Name { get; set; }
  
          [Column("Committee_email")]
          [EmailAddress]
          [Required(ErrorMessage = "Committee must have an Email Address")]
          public string Email { get; set; }
  
          [Column("Committee_inactive")]
          public int? Inactive { get; set; }
  
          [Column("Committee_type")]
          [Required(ErrorMessage = "Committee must have a Type")]
          public string Type { get; set; }
  
          [Column("Committee_description")]
          [MaxLength(1000, ErrorMessage = "Committee Name cannot be longer than 1000 characters")]
          public string Description { get; set; }
  
          [Column("Committee_end_date")]
          public DateTime? EndDate { get; set; }
  
          [Column("bMembershipOpen")]
          [DefaultValue(false)]
          [Display(Name="Membership Open")]
          [Required]
          public bool MembershipOpen { get; set; }
  
          public Person Chairman { get; set; }
  
          public Person Sponsor { get; set; }
  
          public virtual ICollection&lt;Person&gt; Members { get; set; }
  
          public virtual ICollection&lt;Note&gt; Notes { get; set; }  
  
          public virtual ICollection&lt;NextStep&gt; NextSteps { get; set; } 
  
          public virtual ICollection&lt;Outcome&gt; Outcomes { get; set; } 
  
          public virtual ICollection&lt;Video&gt; Videos { get; set; } 
      }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>public class Person {
  
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [Required(ErrorMessage = "You must specify a name")]
      [MaxLength(128, ErrorMessage = "Name cannot be longer than 128 characters")]
      public string Name { get; set; }
  
      [EmailAddress]
      [MaxLength(128, ErrorMessage = "Email Address cannot be longer than 128 characters")]
      public string Email { get; set; }
  
      public virtual ICollection&lt;Committee&gt; Committees { get; set; } 
  }
  </code></pre>
  
  <p>and I have the following in my DbContext</p>
  
  <p>protected override void OnModelCreating(DbModelBuilder modelBuilder) {
          base.OnModelCreating(modelBuilder);</p>
  
  <pre><code>    modelBuilder.Entity&lt;Committee&gt;()
          .HasMany(c =&gt; c.Members)
          .WithMany(m =&gt; m.Committees)
          .Map(m=&gt; {
              m.ToTable("CommitteeMembers");
              m.MapLeftKey("PersonId");
              m.MapRightKey("CommitteeId");
          });
  
      modelBuilder.Entity&lt;Committee&gt;()
          .HasMany(c =&gt; c.Notes)
          .WithRequired(n =&gt; n.Committee);
  
      modelBuilder.Entity&lt;Committee&gt;()
          .HasMany(c =&gt; c.NextSteps)
          .WithRequired(n =&gt; n.Committee);
  
      modelBuilder.Entity&lt;Committee&gt;()
          .HasMany(c =&gt; c.Outcomes)
          .WithRequired(o =&gt; o.Committee);
  
      modelBuilder.Entity&lt;Committee&gt;()
          .HasMany(c =&gt; c.Videos)
          .WithRequired(v =&gt; v.Committee);
  
      modelBuilder.Entity&lt;Committee&gt;()
          .HasRequired(c =&gt; c.Chairman);
  
      modelBuilder.Entity&lt;Committee&gt;()
          .HasRequired(c =&gt; c.Sponsor);
  
      modelBuilder.Entity&lt;Person&gt;()
          .Map(t =&gt; t.ToTable("Persons"));
  }
  </code></pre>
  
  <p>All of my navigation properties work on my models except Committee.Members and Person.Committees. Whenever I try to save the DbContext after adding a Person to a Committee or a Committee to a Person I get an error like the following:</p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint
    "FK_CommitteeMembers_ToCommittee". The conflict occurred in database
    "EquipNetCommitteeDb", table "dbo.Committees", column 'committee_id'.
    The statement has been terminated.</p>
  </blockquote>
  
  <p>Does anyone have any idea what I am doing wrong? This should work. I have done it many times before and I think I just need another set of eyes on it.</p>