Partilhar via


entity set (Entity Data Model)

An entity set is a logical container for instances of an entity type and instances of any type derived from that entity type. (For information about derived types, see Entity Data Model: Inheritance.) The relationship between an entity type and an entity set is analogous to the relationship between a row and a table in a relational database: Like a row, an entity type describes data structure, and, like a table, an entity set contains instances of a given structure. An entity set is not a data modeling construct; it does not describe the structure of data. Instead, an entity set provides a construct for a hosting or storage environment (such as the common language runtime or a SQL Server database) to group entity type instances so that they can be mapped to a data store.

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

For an entity type instance to exist in an entity set, the following must be true:

  • The type of the instance is either the same as the entity type on which the entity set is based, or the type of the instance is a subtype of the entity type.

  • The entity key for the instance is unique within the entity set.

  • The instance does not exist in any other entity set.

    NoteNote:

    Multiple entity sets can be defined using the same entity type, but an instance of a given entity type can only exist in one entity set.

You do not have to define an entity set for each entity type in a conceptual model.

Example

The diagram below shows a conceptual model with three entity types: Book, Publisher, and Author.

Ee382830.ExampleModel(en-us,VS.100).gif

The following diagram shows two entity sets (Books and Publishers) and an association set (PublishedBy) 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 represent a Publisher instance in the Publishers entity set. BiPj represents an instance of the PublishedBy association in the PublishedBy association set.

Ee382830.SetsExample(en-us,VS.100).gif

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 entity set for each entity type in the conceptual model shown above. Note that the name and entity type for each entity 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 entity sets per type (MEST). The following CSDL defines an entity container with two entity sets for the Book entity type:

<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" />
  <AssociationSet Name="PublishedBy" Association="BooksModel.PublishedBy">
    <End Role="Book" EntitySet="Books" />
    <End Role="Publisher" EntitySet="Publishers" />
  </AssociationSet>
  <AssociationSet Name="BookAuthor" Association="BooksModel.BookAuthor">
    <End Role="Book" EntitySet="Books" />
    <End Role="Author" EntitySet="Authors" />
  </AssociationSet>
</EntityContainer>

See Also

Concepts

Entity Data Model Key Concepts

Entity Data Model