Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same entity relationship using FluentNHibernate

I'm trying to create a relationship to the same entity using FluentNHibernate but do not know 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; }
}
like image 735
rafaeldepaula Avatar asked Aug 20 '26 09:08

rafaeldepaula


1 Answers

The mapping could 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: Because Menu instance can have parent, it also could have children. I extended mapping with a children collection which should be declared like this:

public class Menu
{
    ...
    public virtual Menu ParentMenu { get; set; }
    public virtual IList<Menu> ChildMenus { get; set; }
}
like image 151
Radim Köhler Avatar answered Aug 22 '26 05:08

Radim Köhler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!