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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:00:12+00:00 2026-05-29T05:00:12+00:00

I have a class as below: public class Node { public int NodeID {

  • 0

I have a class as below:

    public class Node
    {
         public int NodeID { get; set; }

         public virtual ICollection<Node> Froms { get; set; }
         public virtual ICollection<Node> Tos { get; set; }
    }

When the Code First mapped it to 2 tables:

  1. Nodes
  2. NodesNodes

And two One to Many Relationship between them,

Now the question is how I can made ef to make 3 Tables as below:

  1. Nodes
  2. Froms
  3. Tos

And two One to Many Relationship between Nodes and Froms plus Nodes and Tos

I put two images on my asp.net post that may help
asp.net post

MORE DESCRIPTION

Let me describe more, the existing problem:

I am modeling a pipeline system that has a structure like the NODE I have already mentioned. A pipeline may start from many other pipelines and may ends to many another pipelines, for example: number 1 pipeline starts from number 2,3,4 and ends to number 5 and 6 .

The entity framework mapped the pipeline (Node) class to TWO tables as below:

  Nodes
=========
   ID
---------
   1
   2
   3
   4
   5
   6
         NodeNodes  
============================
Node_NodeID     Node_NodeID1
----------------------------
   1                    2
   1                    3
   1                    4
   5                    1
   6                    1

Great! The EF very smartly put the Starts and Ends in one table, but what the main problem is?!

When a user define starts and ends of number 1 pipeline , if he look at 2,3 and 4 will see they ends to 1 and if he go to 5 and 6 he will see they begins from 1. This additional information (of course they are true) is not desired.

I think about the solution and come up with an idea, if I have two tables for FROMs and TOs ,I won’t have explained problem.

For example:

         Froms  
============================
Node_NodeID     From_NodeID1
----------------------------
   1                    2
   1                    3
   1                    4
         Tos    
============================
Node_NodeID     To_NodeID1
----------------------------
   1                    5
   1                    6
  • 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-05-29T05:00:14+00:00Added an answer on May 29, 2026 at 5:00 am

    In such case your relations cannot be to Node class but you must make new From and To class.

    public class To { ... }
    public class From { ... }
    
    public class Node
    {
         public int NodeID { get; set; }
    
         public virtual ICollection<From> Froms { get; set; }
         public virtual ICollection<To> Tos { get; set; }
    }
    

    Edit:

    EF is not able to map multiple tables to the same entity this way and it also doesn’t make any sense because single node can be from in some cases and to in other cases.

    Edit2:

    Ok. This should be possible but you will have to maintain each relation separately. You must use fluent API to map this. In your context override OnModelCreating method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    {
        modelBuilder.Entity<Node>()
                    .HasMany(n => n.Froms)
                    .WithMany()
                    .Map(m => m.ToTable("Froms"));
    
        modelBuilder.Entity<Node>()
                    .HasMany(n => n.Tos)
                    .WithMany()
                    .Map(m => m.ToTable("Tos"));                    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below : public class Anything { public int Data {
i have class like below class Person { public string Name{get;set;} public string Surname{get;set;}
I have a question, here are two classes below: class Base{ public: virtual void
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
i have a class like below public class Foo<T> { public List<T> Items{ get;
I have a class ComponentItem which i simplified (pseudo code) below public class ComponentItem
I have an entity similar to the below: public class Entity { public List<DateItem>
I have these classes in C# (.NET Framework 3.5) described below: public class Base
I have a data structure which is as given below: class File { public
I have a base class, defined as below (I'm also using DevExpress components): public

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.