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; }
}
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; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With