Same entity relationship using FluentNHibernate

I am trying to create a relationship to the same object using FluentNHibernate, but I have no idea how. Has anyone succeeded? Can you help me?

This is my entity class:

public class Menu
{
    public virtual Guid MenuId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual string Controller { get; set; }
    public virtual int Order { get; set; }
    public virtual Menu ParentMenu { get; set; }
}

      

+3


source to share


1 answer


The mapping can be like this:

public class MenuMap : ClassMap<Menu>
{
  public MenuMap()
  {
      Table("MenuTable");
      Id(x => x.MenuId)
      ...

      // parent
      References(x => x.ParentMenu).Column("ParentId");

      // children, see note below
      HasMany(x => x.ChildMenus)
      .Inverse()
      .KeyColumn("ParentId")
      .Cascade.AllDeleteOrphan()
  }
}

      



NOTE. Since a menu instance can have a parent, it can also have children. I extended the display with a child collection, which should be declared like this:

public class Menu
{
    ...
    public virtual Menu ParentMenu { get; set; }
    public virtual IList<Menu> ChildMenus { get; set; }
}

      

+3


source







All Articles