Wie kann ich Eltern-Kind-Beziehung auf einem Tisch mit Active?
-
23-08-2019 - |
Frage
Wie kann ich Eltern-Kind-Beziehung auf derselben Tabelle?
Id int,
title string,
ParentId int ---> this is refer to Id
Lösung
Was Active Implementierung verwenden Sie?
Castle Active , wenn Sie Ihren Tisch sah wie folgt aus:
table Document (
Id int primary key,
ParentDocumentId int,
Title string
)
werden Sie die folgende Syntax verwenden:
[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; }
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow