this time i have a simple question:
I have one table, related with himself
=====================
| Id |
| IdParent |
| NodeName |
=====================
Where IdParent Could Have 0 or many Id.
The question is, how can i configure this relationship using FluentApi or data anotations.
The result of this relationship is a tree (with two levels) like this:
a
|---b
|---c
d
e
f---g
where (a,d,e,f) are parent nodes and (b,c,g) are child nodes.
Very Tks for your help.
Entity framework supports self-referencing foreign keys. It’s not clear if you are a database first, or codefirst person.
If you’re a database first person, just create a self-referencing foreign key in your database, the update the model from the DB. EF has supported this style of key since at least EF 4.
From a code first perspective, it may a bit trickier. If you’re using EF 4.x you need to override the
OnModelCreatingfunction (see example here). If you’re using EF 5 (from the .net 4.5 beta, or the June 2011 CTP), then this question provides some good guidance. The short of it is to use[ForeignKey("IdParent")]attribute on your model.Either way you should see XML something like …
Self-Referencing Structure
This statement warrents a little attention:
In order to have “zero, or a parentID” then you’re going to need to have an entry in your table containing zero as an ID. Why? because foreign keys need to link to something. What I suspect you want, however, is to make IdParent nullable. Your question doesn’t specify database, but most major databases enforce foreign keys only when there is a value. Said another way, Null in IdParent means there is no parent.