views:

167

answers:

2

I have a table called Area, that stores areas in a hierarchical manner. There is an Id and a ParentId column in the table. The ParentId is linked with a foreign key to the table's Id column.

When I generate the EDM, I get two navigation properties, Area1 and Area2. How am I supposed to get back a tree structure of my areas, and navigate in the tree using LINQ to entities?

I may be asking the wrong question entirely, please bear with me. :)

+1  A: 

Its the right question. When you generate based of the database schema, LINQ autonames the child and parents.

I'd check the designer.cs for your model, but most likely Area1 is the child and Area2 is the Parent. Or its vice versa.

If you want to get all children of a current Area instance:

foreach (Area childArea in currentArea.Area1) {
    // do something
}

If you want the parent:

Area parentArea = currentArea.Area2;

I don't know if you can rename the child and parent, it should be possible. Just haven't dug much into that.

achinda99
+1  A: 

I think that simply renaming the properties will eliminate most of your confusion. One of those properties is the children of the current instance; the other is the parent of the current instance. Look at the mapping details to figure out which is which. Then change the name of the properties to correspond to their function. Now the queries should be much easier to write.

Craig Stuntz