2008-12-20 94 views

回答

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; } 
    } 
}