I have a navigation menu provider that I am trying to move over to EF Code First (EF4, CPT5) using a MenuItem object. This is more of an exercise to get comfortable with building different relationships with EF Code First than anything else.
My MenuItem has a field called SubMenuItems that is a collection of MenuItems. When I use EF Code First (without modifying the classes) the table that is created for the MenuItems adds a column for the parent menu item. The menu will display properly, but this removes any ability to have a menu item appear in more than one sub menu. What is the proper way to tell EF Code First that I want each MenuItem to be a standalone item and to create another table that links a menu item's SubMenuItems to other MenuItems?
public class MenuItem
{
public int ID { get; set; }
public virtual SubMenuItems { get; set; }
public string Text { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public bool IsRoot { get; set; }
public bool RequiresAuthentication { get; set; }
public virtual ICollection<MenuPermission> RequiredPermissions { get; set; }
public bool HiddenIfAuthenticated { get; set; }
public int DisplayOrder { get; set; }
}
...
public class MyDbContext : DbContext
{
public DbSet<MenuItems> MenuItems { get; set; }
public DbSet<MenuItemPermission> MenuItemPermissions { get; set; }
...
}
I have attempted to override OnModelCreating but each attempt ended in either being broken or doing the exact same thing that it does without me touching OnModelCreating. I'm sure I'm just "doing it wrong."
Thanks!