Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same table relationship in Entity Framework

Is it possible to have an association mapping a table to itself?

e.g.

Table:  
ConditionId  
ConditionName 
... 
...  
ParentConditionId

where we can have many ParentConditionIds each mapping to the same ConditionId.

I've tried a one to many mapping but I'm getting an error when there is no children.

like image 780
burnside Avatar asked May 05 '09 12:05

burnside


People also ask

How will you create relationship between tables in Entity Framework?

You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.

What are the different relationship patterns in Entity Framework?

Entity framework supports three types of relationships, same as database: 1) One-to-One 2) One-to-Many, and 3) Many-to-Many.

What is table splitting?

Once you have a table in Word, you might decide to split that table into two or more tables. This way, you can create smaller tables, or add text in between two tables. Put your cursor on the row that you want as the first row of your second table. In the example table, it's on the third row.


2 Answers

Yes it is possible. Put the FK in your database and the wizard will map it correctly. Make sure ParentConditionId is nullable.

like image 155
Craig Stuntz Avatar answered Sep 30 '22 04:09

Craig Stuntz


Take a look tutorial which can be download here . It explains in detail how to model self-reference table.

like image 35
J.W. Avatar answered Sep 30 '22 04:09

J.W.