Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:42:37+00:00 2026-06-01T21:42:37+00:00

I have four entities that I would like to translate into database tables via

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:42:39+00:00Added an answer on June 1, 2026 at 9:42 pm

    …based on the info at hand (keys, relationships are not entirely clear),
    here is the most complex scenario and should cover what you might have I think…

    public class SuggestedMenu
    {
        public int SuggestedMenuId { get; set; }
        public string Description { get; set; }
        public virtual ICollection<MenuCourse> MenuCourses { get; set; }
        // public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; }
    }
    public class MenuCourse
    {
        public int MenuCourseId { get; set; }
        public int SuggestedMenuId { get; set; }
        public SuggestedMenu SuggestedMenu { get; set; }
        public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; }
    }
    public class CourseRecipeChoice
    {
        public int CourseRecipeChoiceId { get; set; }
        public int MenuCourseId { get; set; }
        public int SuggestedMenuId { get; set; }
        public int RecipeId { get; set; }
        // no virtuals if required, non-optional 
        public Recipe Recipe { get; set; }
        public MenuCourse MenuCourse { get; set; }
        // public SuggestedMenu SuggestedMenu { get; set; }
    }
    public class Recipe
    {
        public int RecipeId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public virtual ICollection<CourseRecipeChoice> CourseRecipeChoices { get; set; }
    }
    

    …and in OnModelCreating (I prefer it all config done there, though it’s the same)…

    modelBuilder.Entity<CourseRecipeChoice>()
        .HasKey(crc => new { crc.CourseRecipeChoiceId, crc.SuggestedMenuId, crc.MenuCourseId, crc.RecipeId });
    
    modelBuilder.Entity<CourseRecipeChoice>()
        .HasRequired(r => r.Recipe)
        .WithMany(crc => crc.CourseRecipeChoices)
        .HasForeignKey(crc => crc.RecipeId)
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<CourseRecipeChoice>()
        .HasRequired(m => m.MenuCourse)
        .WithMany(crc => crc.CourseRecipeChoices)
        .HasForeignKey(crc => new { crc.MenuCourseId, crc.SuggestedMenuId })
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<SuggestedMenu>()
        .HasKey(crc => crc.SuggestedMenuId );
    
    modelBuilder.Entity<MenuCourse>()
        .HasKey(crc => new { crc.MenuCourseId, crc.SuggestedMenuId });
    
    modelBuilder.Entity<MenuCourse>()
        .HasRequired(m => m.SuggestedMenu)
        .WithMany(crc => crc.MenuCourses)
        .HasForeignKey(crc => crc.SuggestedMenuId)
        .WillCascadeOnDelete(false);
    
    modelBuilder.Entity<Recipe>()
        .HasKey(crc => crc.RecipeId );
    

    …and to test e.g. something like…

            using (var db = new YourDbContext())
            {
                SuggestedMenu suggestedmenu = new SuggestedMenu { Description = "suggested menu" };
                var menucourse = new MenuCourse { MenuCourseId = 2, SuggestedMenu = suggestedmenu };
                var recipe = new Recipe { Name = "My recipe", Description = "recipe desc" };
                var crc = new CourseRecipeChoice { CourseRecipeChoiceId = 2, MenuCourse = menucourse, Recipe = recipe, };
                db.CourseRecipeChoices.Add(crc);
                int recordsAffected = db.SaveChanges();
                foreach (var crcs in db.CourseRecipeChoices.Include(c => c.MenuCourse).Include(c => c.Recipe))
                {
                    Console.WriteLine("{0}, {1}, {2}, {3}", crcs.MenuCourse.MenuCourseId, crcs.MenuCourse.SuggestedMenuId, crcs.Recipe.Name, crcs.Recipe.Description);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement code-first approach of entity framework. I have four entities
i have four tables user-question contains two columns: questionID, userID, the questions that the
I have four entities that are involved in a query that I'm having a
Imagine that I have UserId (actually we do have roughly four columns like userId,
This is my database structure I have four tables. Table LocalArea and Lanungaue have
I have four tables. Three of them are tables with one key and they
i have four imageviews in a linear layout in such a manner that only
I have four textfields that bind to the model key path. If a number
Let me explain my problem: I have four tables created as objects using Entity
I have four input boxes. If the user fills the first box and clicks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.