1. 程式人生 > 實用技巧 >3.5 建立模型-關係

3.5 建立模型-關係

術語定義

有許多術語用於描述關係

  • 相關實體: 這是包含外來鍵屬性的實體。 有時稱為關係的 "子級"。
  • 主體實體: 這是包含主/備用鍵屬性的實體。 有時稱為關係的 "父項"。
  • 主體金鑰: 唯一標識主體實體的屬性。 這可能是主鍵或備用金鑰。
  • 外來鍵: 用於儲存相關實體的主體鍵值的依賴實體中的屬性。
  • 導航屬性: 在主體和/或從屬實體上定義的屬性,該屬性引用相關實體。
    • 集合導航屬性: 一個導航屬性,其中包含對多個相關實體的引用。
    • 引用導航屬性: 儲存對單個相關實體的引用的導航屬性。
    • 反向導航屬性: 討論特定導航屬性時,此術語是指關係另一端的導航屬性。
  • 自引用關係: 依賴關係和主體實體型別相同的關係。

下面的程式碼顯示與之間的一對多關係 Blog``Post

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}
  • Post是依賴實體
  • Blog是主體實體
  • Blog.BlogId是主體鍵(在本例中為主金鑰,而不是備用金鑰)
  • Post.BlogId為外來鍵
  • Post.Blog是一個引用導航屬性
  • Blog.Posts是集合導航屬性
  • Post.Blog是的反向導航屬性 Blog.Posts (反之亦然)