【问题标题】:How can I set parent-child relation on same table using ActiveRecord?如何使用 ActiveRecord 在同一张表上设置父子关系?
【发布时间】:2010-09-27 19:59:45
【问题描述】:

如何在同一张表上设置父子关系?

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

【问题讨论】:

  • 谁知道答案请告诉我

标签: parentid


【解决方案1】:

您使用的是什么 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; }
    }
}

【讨论】:

    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多