Suppose you have a class named MyClas开发者_如何学Cs
. 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?
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:
- http://ayende.com/Blog/archive/2006/11/22/ComplexQueriesWithActiveRecord.aspx
- http://web.archive.org/web/20090806071731/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx
- http://ayende.com/Blog/archive/2009/08/28/nhibernate-tips-amp-tricks-efficiently-selecting-a-tree.aspx
精彩评论