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).
Here's the message I'm getting:
"One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical."
Here's what I've tried in my EntityTypeConfiguration class and it's obviously incorrect...
public class CourseRecipeChoiceConfiguration : EntityTypeConfiguration<CourseRecipeChoice>
{
public CourseRecipeChoiceConfiguration()
{
HasKey(crc => new { crc.Id});
HasRequired(r => r.Recipe).WithMany(crc => crc.CourseRecipeChoices).HasForeignKey(crc => crc.RecipeId);
HasRequired(m => m.MenuCourse).WithMany(crc => crc.CourseRecipeChoices).HasForeignKey(crc => crc.MenuCourseId);
HasRequired(m => m.MenuCourse).WithMany(crc => crc.CourseRecipeChoices).HasForeignKey(crc => crc.SuggestedMenu_MenuCourseId);
}
}
What is the correct syntax for the navigation properties and the correct syntax for fluent api syntax for the CourseRecipeChoice join table?
public class SuggestedMenu
{
public int SuggestedMenuId { get; set; }
public virtual ICollection<MenuCourse> MenuCourses { get; set; }
}
public class MenuCourse
{
public int Id { get; set; }
public int SuggestedMenuId { get; set; }
public SuggestedMenu SuggestedMenu { get; set; }
public virtual ICollection<CourseRecipeChoice> 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<CourseRecipeChoice> CourseRecipeChoices { get; set; }
}
The keys are as follows:
- SuggestedMenu(Id)
- MenuCourse(Id, SuggestedMenuId)
- CourseRecipeChoice(Id, SuggestedMenuId, MenuCourseId, RecipeId) //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?)
- Recipe(RecipeId)