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 8186447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:12:04+00:00 2026-06-07T02:12:04+00:00

From examples that I have seen online and in a Programming Entity Framework CodeFirst

  • 0

From examples that I have seen online and in a Programming Entity Framework CodeFirst book, when you have a collection on both classes EF would create a mapping table such as MembersRecipes and the primary key from each class would link to this table.

However when I do the below, I instead get a new field in the Recipes table called Member_Id and a Recipe_Id in the Members table.

Which only creates two one-to-many relationships, but not a many-to-many so I could have Member 3 linked to Recipes (4,5,6) and Recipe 4 linked to Members (1,2,3) etc.

Is there a way to create this mapping table? and if so how do you name it something else such as “cookbooks” ?

Thanks

    public abstract class Entity {
        [Required]
        public int Id { get; set; }
    }   

    public class Member : Entity {
        [Required]
        public string Name { get; set; }

        public virtual IList<Recipe> Recipes { get; set; }
    }

    public class Recipe : Entity {  
        [Required]
        public string Name { get; set; }

        [ForeignKey("Author")]
        public int AuthorId { get; set; }
        public virtual Member Author { get; set; }

            ....

        public virtual IList<Member> Members { get; set; }
    }

UPDATE:
Below is another approach I have tried which doesn’t use the Fluent API and replaces the AuthorId & Author on Recipe with an owner flag, I have also renamed the below example from Cookbooks to MembersRecipes, this also fixes my issue similar to the answer but as mentioned has further implications.

public class MembersRecipes {

    [Key, Column(Order = 0)]
    [ForeignKey("Recipe")]
    public int RecipeId { get; set; }
    public virtual Recipe Recipe { get; set; }

    [Key, Column(Order = 1)]
    [ForeignKey("Member")]
    public int MemberId { get; set; }
    public virtual Member Member { get; set; }

    public bool Owner { get; set; }
}

and in Recipe & Member classes I changed the collections to

public virtual IList<MembersRecipes> MembersRecipes { get; set; }
  • 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-07T02:12:07+00:00Added an answer on June 7, 2026 at 2:12 am

    Do this on your DbContext OnModelCreating:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {    
        modelBuilder.Entity<Recipe>()
            .HasMany(x => x.Members)
            .WithMany(x => x.Recipes)
        .Map(x =>
        {
            x.ToTable("Cookbooks"); // third table is named Cookbooks
            x.MapLeftKey("RecipeId");
            x.MapRightKey("MemberId");
        });
    }
    

    You can do it the other way around too, it’s the same, just another side of the same coin:

    modelBuilder.Entity<Member>()
        .HasMany(x => x.Recipes)
        .WithMany(x => x.Members)
    .Map(x =>
    {
      x.ToTable("Cookbooks"); // third table is named Cookbooks
      x.MapLeftKey("MemberId");
      x.MapRightKey("RecipeId");
    });
    

    Further examples:

    http://www.ienablemuch.com/2011/07/using-checkbox-list-on-aspnet-mvc-with_16.html

    http://www.ienablemuch.com/2011/07/nhibernate-equivalent-of-entity.html


    UPDATE

    To prevent cyclical reference on your Author property, aside from above, you need to add this:

    modelBuilder.Entity<Recipe>()
        .HasRequired(x => x.Author)
        .WithMany()
        .WillCascadeOnDelete(false);
    

    Idea sourced here: EF Code First with many to many self referencing relationship

    The core thing is, you need to inform EF that the Author property(which is a Member instance) has no Recipe collections(denoted by WithMany()); that way, cyclical reference could be stopped on Author property.

    These are the created tables from the Code First mappings above:

    CREATE TABLE Members(
        Id int IDENTITY(1,1) NOT NULL primary key,
        Name nvarchar(128) NOT NULL
    );
    
    
    CREATE TABLE Recipes(
        Id int IDENTITY(1,1) NOT NULL primary key,
        Name nvarchar(128) NOT NULL,
        AuthorId int NOT NULL references Members(Id)
    );
    
    
    CREATE TABLE Cookbooks(
        RecipeId int NOT NULL,
        MemberId int NOT NULL,
        constraint pk_Cookbooks primary key(RecipeId,MemberId)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code examples from some of my previous work that help me to
I have found that in all examples (include rails documentation) that I have seen
I have seen two examples that illustrate how the client socket can receive messages
Is it just me or literally ALL the examples that I have seen on
Say I have components that I need to iterate through, for example From and
I have a "simple" 4 class example that reliably shows unexpected behavior from java
I currently have a csv file that I'm parsing with an example from here:
Example: I have an class that inherits from UIImageView. An object creates an instance
For example I have a dropdown box that gets a town from the table
I have some tables that benefit from many-to-many tables. For example the team table.

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.