castle-activerecordrecursioncastle

How to define recursive Property in Castle ActiveRecord?


Suppose you have a class named MyClass. MyClass should have a property named Parent, Parent must be of type MyClass itself. It is necessary because MyClass wants to hold a tree structure.

How can it be done?


Solution

  • It's pretty straightforward:

    [ActiveRecord(Lazy = true)]
    public class MyClass {
      [BelongsTo]
      public virtual MyClass Parent { get;set; }
    }
    

    You might also want to map the collection of children.

    See these articles for more information on how to run recursive queries over this: