title: Code First Fluent API and Navigation Properties in a Join Table
body: <p>I have four entities that I would like to translate into database tables via code first fluent api (I'm using a model found at databaseanswers.org), but I'm not certain as to how. The problem I'm having is that SuggestedMenuId is being migrated across two different tables in a Composite key (MenuCourse and CourseRecipeChoice).</p>
  
  <p>Here's the message I'm getting:</p>
  
  <p>"One or more validation errors were detected during model generation:</p>
  
  <p>\tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical."</p>
  
  <p>Here's what I've tried in my EntityTypeConfiguration class and it's obviously incorrect...</p>
  
  <pre><code>public class CourseRecipeChoiceConfiguration : EntityTypeConfiguration&lt;CourseRecipeChoice&gt;
  {
      public CourseRecipeChoiceConfiguration()
      {
          HasKey(crc =&gt; new { crc.Id});
          HasRequired(r =&gt; r.Recipe).WithMany(crc =&gt; crc.CourseRecipeChoices).HasForeignKey(crc =&gt; crc.RecipeId);
          HasRequired(m =&gt; m.MenuCourse).WithMany(crc =&gt; crc.CourseRecipeChoices).HasForeignKey(crc =&gt; crc.MenuCourseId);
          HasRequired(m =&gt; m.MenuCourse).WithMany(crc =&gt; crc.CourseRecipeChoices).HasForeignKey(crc =&gt; crc.SuggestedMenu_MenuCourseId);
      }
  }
  </code></pre>
  
  <p>What is the correct syntax for the navigation properties and the correct syntax for fluent api syntax for the CourseRecipeChoice join table?</p>
  
  <pre><code>public class SuggestedMenu
  {
      public int SuggestedMenuId { get; set; }
  
      public virtual ICollection&lt;MenuCourse&gt; MenuCourses { get; set; }
  }
  
  public class MenuCourse
  {
      public int Id { get; set; }
      public int SuggestedMenuId { get; set; }
  
      public SuggestedMenu SuggestedMenu { get; set; }
      public virtual ICollection&lt;CourseRecipeChoice&gt; CourseRecipeChoices { get; set; }
  }
  
  public class CourseRecipeChoice
  {
      public int SuggestedMenuId { get; set; }
      public int MenuCourseId { get; set; }
      public int Id { get; set; }
      public int RecipeId { get; set; }
  
      //How do I represent the navigation properties in this class? 
  
  }
  
  public class Recipe
  {
      public int RecipeId { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
  
      public ICollection&lt;CourseRecipeChoice&gt; CourseRecipeChoices { get; set; }
  }
  </code></pre>
  
  <p>The keys are as follows:</p>
  
  <ul>
  <li><strong>SuggestedMenu(Id)</strong></li>
  <li><strong>MenuCourse(Id, SuggestedMenuId)</strong></li>
  <li><strong>CourseRecipeChoice(Id, SuggestedMenuId, MenuCourseId, RecipeId)</strong> //this is actually where I get confused because according to the model, SuggestedMenuId is a PK in SuggestedMenu and a PF in MenuCourse and CourseRecipeChoice (could this just be bad design?)</li>
  <li><strong>Recipe(RecipeId)</strong></li>
  </ul>