共用方式為


Association Set - 關聯集

關聯集是相同類型的關聯執行個體的邏輯容器。 關聯集不是資料模型建構,也就是說,它不會描述資料或關聯性的結構。 反之,關聯集會提供建構,讓裝載或儲存環境 (例如 Common Language Runtime 或 SQL Server 資料庫) 群組關聯執行個體,以將其對應至資料存放區。

關聯集是在實體容器中定義的,此實體容器是實體集和關聯集的邏輯群組。

關聯集的定義包含下列資訊:

  • 關聯集名稱。 (必要項)

  • 將包含執行個體的關聯。 (必要項)

  • 兩個關聯集 End

範例

下圖顯示包含兩個關聯 (PublishedByWrittenBy) 的概念模型。 雖然圖中並未提供關聯集的相關資訊,但下圖會顯示以此模型為基礎的關聯集和實體集範例。

Example model with three entity types

下列範例顯示以前述概念模型為基礎的一個關聯集 (PublishedBy) 和兩個實體集 (BooksPublishers)。 Books 實體集中的 Bi 代表執行階段時的 Book 實體類型執行個體。 同樣地,Pj 則代表 Publishers 實體集中的 Publisher 執行個體。 BiPj 代表 PublishedBy 關聯集內的 PublishedBy 關聯的執行個體。

Screenshot that shows a Sets example.

ADO.NET Entity Framework 會使用稱為概念結構定義語言 (CSDL) 的特定領域語言 (DSL) 來定義概念模型。 下列 CSDL 定義上圖所示之實體實體容器,每個關聯具有一個關聯集。 請注意,每個關聯集的名稱和關聯都是使用 XML 屬性定義的。

<EntityContainer Name="BooksContainer" >
  <EntitySet Name="Books" EntityType="BooksModel.Book" />
  <EntitySet Name="Publishers" EntityType="BooksModel.Publisher" />
  <EntitySet Name="Authors" EntityType="BooksModel.Author" />
  <AssociationSet Name="PublishedBy" Association="BooksModel.PublishedBy">
    <End Role="Book" EntitySet="Books" />
    <End Role="Publisher" EntitySet="Publishers" />
  </AssociationSet>
  <AssociationSet Name="WrittenBy" Association="BooksModel.WrittenBy">
    <End Role="Book" EntitySet="Books" />
    <End Role="Author" EntitySet="Authors" />
  </AssociationSet>
</EntityContainer>

每個關聯可以定義多個屬性集,前提是所有關聯集皆不共用關聯集 End。 下列 CSDL 可定義具有兩個 WrittenBy 關聯之關聯集的實體容體。 請注意,BookAuthor 實體類型皆定義了多個實體集,且所有關聯集皆不共用關聯集 End。

<EntityContainer Name="BooksContainer" >
  <EntitySet Name="Books" EntityType="BooksModel.Book" />
  <EntitySet Name="FictionBooks" EntityType="BooksModel.Book" />
  <EntitySet Name="Publishers" EntityType="BooksModel.Publisher" />
  <EntitySet Name="Authors" EntityType="BooksModel.Author" />
  <EntitySet Name="FictionAuthors" EntityType="BooksModel.Author" />
  <AssociationSet Name="PublishedBy" Association="BooksModel.PublishedBy">
    <End Role="Book" EntitySet="Books" />
    <End Role="Publisher" EntitySet="Publishers" />
  </AssociationSet>
  <AssociationSet Name="WrittenBy" Association="BooksModel.WrittenBy">
    <End Role="Book" EntitySet="Books" />
    <End Role="Author" EntitySet="Authors" />
  </AssociationSet>
  <AssociationSet Name="FictionWrittenBy" Association="BooksModel.WrittenBy">
    <End Role="Book" EntitySet="FictionBooks" />
    <End Role="Author" EntitySet="FictionAuthors" />
  </AssociationSet>
</EntityContainer>

另請參閱