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

  • Home
  • SEARCH
  • 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 8490697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:12:05+00:00 2026-06-10T22:12:05+00:00

How can I implement a child that has multiple parents in Entity Framework? The

  • 0

How can I implement a child that has multiple parents in Entity Framework?
The resulting tables must be as follows:

1.Courses:

CourseID int identity
CourseTitle nvarchar
.
.
.

OtherColumns as neede

2.CoursePreRequisites:

CourseID (FK to Course.CourseID)
PreRequisiteCourseID (FK to Course.CourseID)

or is there any better way to achieve multiple parent for a child record?

  • 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-10T22:12:06+00:00Added an answer on June 10, 2026 at 10:12 pm

    You just need two navigation properties in the child class refering to the same parent class and – optionally – two corresponding foreign key properties:

    public class Course
    {
        public int CourseID { get; set; } // PK property
        public string CourseTitle { get; set; }
    }
    
    public class CoursePreRequisite
    {
        public int CoursePreRequisiteID { get; set; } // PK property
    
        public int CourseID { get; set; }  // FK property 1
        public Course Course { get; set; } // Navigation property 1
    
        public int PreRequisiteCourseID { get; set; }  // FK property 2
        public Course PreRequisiteCourse { get; set; } // Navigation property 2
    }
    

    If one or both of the two relationships are optional, use int? instead of int for the foreign key properties.

    If you use the property names as indicated in the example above you don’t need to configure anything. EF will recognize the two one-to-many relationships by naming conventions.

    You can also use collections as inverse properties in the Course entity if you need or want them:

    public class Course
    {
        public int CourseID { get; set; } // PK property
        public string CourseTitle { get; set; }
    
        public ICollection<CoursePreRequisite> PreRequisites1 { get; set; }
        public ICollection<CoursePreRequisite> PreRequisites2 { get; set; }
    }
    

    However, in that case you must specify which navigation property pairs belong together in a relationship. You can do this with data annotations for example:

        [InverseProperty("Course")]
        public ICollection<CoursePreRequisite> PreRequisites1 { get; set; }
        [InverseProperty("PreRequisiteCourse")]
        public ICollection<CoursePreRequisite> PreRequisites2 { get; set; }
    

    Or with Fluent API:

    modelBuilder.Entity<Course>()
        .HasMany(c => c.PreRequisites1)
        .WithRequired(p => p.Course)      // Or WithOptional
        .HasForeignKey(p => p.CourseID);
    
    modelBuilder.Entity<Course>()
        .HasMany(c => c.PreRequisites2)
        .WithRequired(p => p.PreRequisiteCourse)      // Or WithOptional
        .HasForeignKey(p => p.PreRequisiteCourseID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to implement in rails a category that can be child of another category
I know that classes can implement various special methods, such as __iter__ , __setitem__
I'd like to know if I can implement a method on MemoryCache that removes
I'm trying to implement the following: I have an Items Manager, that has an
We have an interface that has a child interface. Ie: public interface GenCalculator extends
I have 2 entities in a to-many relationship, Parent-->Child. The Child entity has an
I have a BCS model in SharePoint. This model has a single entity that
I am trying to implement some tables that mimic inheritance in a relational (sqlite)
I can implement both variants - it's easy. But I'm interested: what approach is
How can implement reordring in winforms datagridview by using the feature of drag and

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.