I have Menu table in database which have self referencing foreign key i.e. ParentID. Below is my Menu class (DB First Approach)
public partial class Menu
{
public Menu()
{
this.Menu1 = new HashSet<Menu>();
this.Products = new HashSet<Product>();
}
public int MenuID { get; set; }
public string Name { get; set; }
public Nullable<int> ParentID { get; set; }
public virtual ICollection<Menu> Menu1 { get; set; }
public virtual Menu Menu2 { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
I want to implement following things,
- I want entire hierarchy using menu id e.g "if I pass 7 then result should be all the child and sub-child of menu id 7"
- If I pass 7 then I want all parent and super parent of menu id 7.
I found several article on StackOverflow before posting this question but they were asking to implement Code First Approach. Here are the questions posted on Stackoverflow before Entity Framework Self Join, Most efficient method of self referencing tree using Entity Framework