كيف يمكنني تعيين علاقة الوالدين والطفل على نفس الجدول باستخدام activerecord؟

StackOverflow https://stackoverflow.com/questions/382976

  •  23-08-2019
  •  | 
  •  

سؤال

كيف يمكنني تعيين علاقة الوالدين والطفل على نفس الجدول؟

Id int, 
title string, 
ParentId int  ---> this is refer to Id
هل كانت مفيدة؟

المحلول

ما تنفيذ activerecord الذي تستخدمه؟

في قلعة 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; }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top