gpt4 book ai didi

parentid - 如何使用 ActiveRecord 在同一张表上设置父子关系?

转载 作者:行者123 更新时间:2023-12-02 02:38:11 26 4
gpt4 key购买 nike

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

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

最佳答案

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

关于parentid - 如何使用 ActiveRecord 在同一张表上设置父子关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/382976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com