Entity framework code first recursive relationship

How to Configure a Self Referencing Entity in Code First - CodeProject

many table name with this code: protected override void OnModelCreating( ModelBuilder modelBuilder) { raznomir.info().HasMany(m => m. I have been doing a series of post about Entity Framework Code First and in one of the post I explained various types of relationships. Recursive or hierarchical queries using EF Code First and Migrations · Get SDK and tools to Entities, raznomir.info Entity Framework Code First.

Here's the Up method that is generated by Code First Migrations for the two entities: MenuId ; CreateTable "dbo. Here's some code to be executed in the Seed method that creates a Menu object and adds some MenuItems to it: MenuName, menu ; context.

SaveChanges ; The code creates five parent menu items. Then it creates three further items with a ParentMenuItemId specified.

entity framework - One to many recursive relationship with Code First - Stack Overflow

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: 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: MenuOrder ; if items. 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. However, we really want to add a few more properties in order to make this class useful.

For example, if you are a child category you really want to be able to use dot notation to get the name of the parent category e. Using EF, we will create a virtual property named Parent.

By making the property virtual we are letting EF know that when this property is accessed we want to load some data. Based on your configuration settings and the code you use to retrieve your data whether or not you used DbSet. IncludeEF will lazy load or eager load this data. Once again, here is the modified class: In database speak, this simply means that the ParentID field is nullable.

Recursive or hierarchical queries using EF Code First and Migrations

The code also states that if a Category object can have zero or many children. As I mentioned earlier, if you are a programmer its easier to think of the ParentId field as a pointer.

Finally, I disabled the cascade on delete option. This step is optional and probably based on your own personal preferences. If you enable cascade on delete and you delete a category that has children then you will effectively remove records. For whatever reason this scares me a little bit.

Perhaps, my short career as a DBA caused me to not trust people with large volume delete statements. However, you may decide differently depending on your circumstances. Hopefully, this short EF tutorial will help you if you are working through a scenario where you need to capture and manipulate hierarchical data.