Muokkaa

Jaa


association set

An association set is a logical container for association instances of the same type. An association set is not a data modeling construct; that is, it does not describe the structure of data or relationships. Instead, an association set provides a construct for a hosting or storage environment (such as the common language runtime or a SQL Server database) to group association instances so that they can be mapped to a data store.

An association set is defined within an entity container, which is a logical grouping of entity sets and association sets.

A definition for an association set contains the following information:

  • The association set name. (Required)

  • The association of which it will contain instances. (Required)

  • Two association set ends.

Example

The diagram below shows a conceptual model with two associations: PublishedBy, and WrittenBy. Although information about association sets is not conveyed in the diagram, the next diagram shows an example of association sets and entity sets based on this model.

Example model with three entity types

The following example shows an association set (PublishedBy) and two entity sets (Books and Publishers) based on the conceptual model shown above. Bi in the Books entity set represents an instance of the Book entity type at run time. Similarly, Pj represents a Publisher instance in the Publishers entity set. BiPj represents an instance of the PublishedBy association in the PublishedBy association set.

Screenshot that shows a Sets example.

The ADO.NET Entity Framework uses a domain-specific language (DSL) called conceptual schema definition language (CSDL) to define conceptual models. The following CSDL defines an entity container with one association set for each association in the diagram above. Note that the name and association for each association set are defined using XML attributes.

<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>

It is possible to define multiple association sets per association, as long as no two association sets share an association set end. The following CSDL defines an entity container with two association sets for the WrittenBy association. Notice that multiple entity sets have been defined for the Book and Author entity types and that no association set shares an association set 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>

See also