3

同じテーブルに親子関係を設定するにはどうすればよいですか?

Id int, 
title string, 
ParentId int  ---> this is refer to Id
4

1 に答える 1

2

どのActiveRecord実装を使用していますか?

Castle ActiveRecordで、テーブルが次のようになっている場合:

table Document (
   Id int primary key,
   ParentDocumentId int,
   Title string
)

次の構文を使用します。

[ActiveRecord(Table = "Document")]
public class Document : ActiveRecordBase<Document> {

    private int id;
    private Document parent;
    private string title;
    private List<Document> children = new List<Document>();

    [PrimaryKey]
    public int Id {
        get { return id; }
        set { id = value; }

    }

    [BelongsTo("ParentDocumentId")]
    public virtual Document Parent {
        get { return parent; }
        set { parent = value; }
    }

    [HasMany(Table = "Document", ColumnKey = "ParentDocumentId", Inverse = true, Cascade = ManyRelationCascadeEnum.All)]
    public IList<Document> Children {
        get { return children.AsReadOnly(); }
        private set { children = new List<Document>(value); }
    }

    [Property]
    public string Title {
        get { return title; }
        set { title = value; }
    }
}
于 2008-12-20T17:17:59.023 に答える