UK Hosting ASP.NET – How to Use Entity Framework 6 to Manage & Display Hierarchical Data

How to Use Entity Framework 6 to Manage & Display Hierarchical Data

Entity Framework (EF) is an open source object-relational mapping (ORM) framework for ADO.NET, part of .NET Framework. Entity Framework is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write.

This article explores how to use Entity Framework 6 to manage the retrieval and display of hierarchical data such as you might find in a menu system. There are a number of existing items on the Internet that cover the same topic, but most of them seem unnecessarily complex and don’t include any information about displaying the data once it has been retrieved.

Each menu item has a ParentMenuItemId which is nullable. If a specific item is a top level item, the value will be null. Otherwise the value will be the item to which it is related. The MenuId links the items to a specific menu. This is how the two entities are defined using C#:

Menu Entity

MenuItem Entity

The self-referencing relationship is defined through the nullable ParentMenuItemId property paired with the virtual Parent property. Any children are taken care of through the virtual ICollection<MenuItem> Children property. By convention, that’s all Code First Migrations needs in order to generate the appropriate tables and keys. Here’s the Up method that is generated by Code First Migrations for the two entities:

The ParentMenuItemId is nullable, which is what enables top level elements to exist. Here’s some code to be executed in the Seed method that creates a Menu object and adds some MenuItems to it:

The code creates five parent menu items. Then it creates three further items with a ParentMenuItemId specified. These become children of the first parent item. A further three items are created and related to the item with the MenuItemId of 7, which will actually be the second child item. The LINQ required to retrieve the menu items is very simple:

And the SQL that this generates is also very simple:

You could choose to explicitly include the Children navigational property:

This will alter the SQL to the following:

It enables population of the Children properties and is only necessary if you needed to reference them in your code. You might want to do this if you prefer to take a strictly object oriented approach to coding. However, all you really need to display this data is a recursive helper like the following BuildMenu method:


The menu data is passed in to the helper the first time with a parentId value being omitted and therefore defaulting to null. The menu items are queried for all those that have a ParentMenuItemid that matches the parentId value, which on the first iteration yields all those that are root items. If there are any matches, an unordered list is created and each item is displayed as a list item with a link. As each item is rendered, the entire collection is passed back into the BuildMenu method with the parentId parameter set to the id of the current item, so that a check for any children belonging to the current item can be performed. The resulting output looks like this:


Best and Cheap ASP.NET Entity Framework 6 Hosting

To help you find the best Windows hosting provider that fully support ASP.NET, we will give you our recommendation. After we had reviewed 50+ providers, we found that ASPHostPortal is one of the best. They have expert ASP.NET support who always ready to help your problem, although with 99.99% uptime we think you won’t get any problem.

[stextbox id=”asp_net_hosting” caption=” is Microsoft No #1 Recommended Windows Hosting Partner”] is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to for the ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, .NET 4.5.2/ASP.NET 4.5.1, ASP.NET MVC 6.0/5.2, Silverlight 5 and Visual Studio Lightswitch. Click here for more information[/stextbox]

Anjali Punjab

Anjali Punjab is a freelance writer, blogger, and ghostwriter who develops high-quality content for businesses. She is also a HubSpot Inbound Marketing Certified and Google Analytics Qualified Professional.

You may also like...

Popular Posts