开发者

Mapping complex tree object using fluent mappings in EF 4.1?

开发者 https://www.devze.com 2023-04-06 09:35 出处:网络
I need some assistance with how to properly mapping this structure in EF 4.1: public class Menu: Entity

I need some assistance with how to properly mapping this structure in EF 4.1:

public class Menu: Entity
    {
        public string Title { get; set; }        
        public virtual ICollection<MenuItem> MenuItems { get; set; } 
    }

 public class MenuItem: Entity
    {
        public string Icon { get; set; }
        public string Text { get; set; }
        public string Action { get; 开发者_JAVA技巧set; }
        public string Controller { get; set; }
        public string Parameters { get; set; }
        public virtual MenuItemType Type { get; set; }
        public virtual Guid? ContextMenuId { get; set; }
        public virtual Menu ContextMenu { get; set; }
        public virtual Guid? ParentMenuItemId { get; set; }
        public virtual MenuItem ParentMenuItem { get; set; }
        public virtual ICollection<MenuItem> ChildMenuItems { get; set; }
    }

The Entity base class has the ID for the enitties and I also have a base mapping class that builds the mappings for the key. Here is what I have so far for the MenuItem class:

public class MenuItemMapping : EntityConfiguration<MenuItem>
    {
        public MenuItemMapping()
        {
            HasOptional(mi => mi.ParentMenuItem).WithMany(p => p.ChildMenuItems).HasForeignKey(mi => mi.ParentMenuItemId).WillCascadeOnDelete(false);
            HasOptional(mi => mi.ContextMenu).WithMany().HasForeignKey(mi => mi.ContextMenuId).WillCascadeOnDelete(false);
        }
    }

My concern is in the ContextMenu because it is a Menu type and not sure the best way to handle this type o mapping.

Update

Well, I added an additional mapping for the Menu (in a MenuMapping class similar to the above mapping class) for the collection of Menuitems and it seems to be OK, but I'd still like to know if what I am doing is correct.


Apparently, my mappings were fine. I thought I would have issues with circular references.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号