Condividi tramite


Entity Sets (EDM)

In the Entity Data Model (EDM), an EntitySet is a logical container for entities of a single type. Similarly, an AssociationSet is a container for associations of the same type. Entity and association sets defined in schemas are mapped to tables in the database that store data for applications. Entity and association sets are the basis of classes in the programming object model that will be used by application code.

Entity sets and association sets define the scope of entities and associations; entity containers define the storage containers that hold entities and associations. There are no sets of SimpleType. These types are instantiated as values assigned to properties of an entity.

An EntitySet for an EntityType contains instances of the EntityType or any one of its subtypes. More than one EntitySet can be defined by using the same EntityType. An instance of an EntityType can only be a member of one EntitySet.

Entity instances in an EntitySet must satisfy three conditions:

  • The type of the entity instance is either the EntityType of the EntitySet or any subtype of the EntityType.

  • The Key value of each entity instance uniquely identifies it in the EntitySet.

  • The entity instance is not a member of any other EntitySet.

The following conceptual schema definition language (CSDL) syntax is the declaration of an EntitySet named CustomerSet. The EntitySet contains instances of the entity CustomerType:

<EntitySet Name="CustomerSet" EntityType="CustomerType"/>

In the following schema segment, two EntityType declarations define the entity types Product and Supplier. The entity sets based on Product and Supplier entities are named appropriately in the plural forms: Products and Suppliers. These entity sets are added to the definition of an EntityContainer.

<?xml version="1.0" encoding="utf-8"?>
<Schema xmlns:cg="https://schemas.microsoft.com/ado/2006/04/codegeneration"
    xmlns:edm="https://schemas.microsoft.com/ado/2006/04/edm"
    xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
    Namespace="MyCompany.LOBSchema" Alias="Self">

<EntityType Name="Product">
    <Key>
      <PropertyRef Name="ProductID" />
    </Key>
    <Property Name="ProductID" Type="Int32" Nullable="false" />
    <Property Name="ProductName" Type="String" Nullable="false" />
    <Property Name="UnitPrice" Type="Decimal" Nullable="true" />
    <Property Name="UnitsInStock" Type="Int16" Nullable="true" />
</EntityType>

<EntityType Name="Supplier">
    <Key>
      <PropertyRef Name="SupplierID" />
    </Key>
    <Property Name="SupplierID" Type="Int32" Nullable="false" />
    <Property Name="CompanyName" Type="String" Nullable="false" />
    <Property Name="ContactName" Type="String" Nullable="true" />
    <Property Name="HomePage" Type="String" Nullable="true" />
</EntityType>

<EntityContainer Name="LOB-Data">
    <EntitySet Name="Products" EntityType="Product" />
    <EntitySet Name="Suppliers" EntityType="Supplier" />
</EntityContainer>

</Schema>

For more information about entity containers, see Entity Containers (EDM).

Multiple Entity Sets per Type

The Entity Data Model (EDM) allows multiple entity sets within a single entity container or multiple entity containers to be defined for the same entity type. Defining multiple entity sets per type (MEST) allows users to streamline their code when databases have partitioning or other such scenarios where multiple tables have the same structure.

Databases are partitioned for a variety of reasons such as performance, availability, or manageability. A financial institution that manages accounts for a large number of customers might partition their database systems for performance by a regional distribution of customer data. Searching and updating the data will be faster when restricted to a regional subset of the data. To implement MEST the partitioning scenario must occur within the same database. If partitioning occurs across different databases, different connection strings and different contexts will be required instead of a MEST scenario.

Another scenario where partitioning might be useful is the case where an IT department wants to back up frequently-accessed data on a nightly basis, while data that has not been accessed in more than a year gets archived. The DBAs might archive customers who haven’t used their account data in more a year to an archive table in the same database with the same table schema.

It is important to consider the structure of associations between entity types that are members of more than one entity set. MEST is most easily implemented according to structure that has been tested. The following scenarios have been used successfully.

  • In a one-to-many association, an entity implemented as MEST on the "one" end of an association should have a relationship with two different entity sets on the "many" end. For example, the following scenario can be implemented with the Customer entity type in two entity sets: PreferedCustomers and CreditRiskCustomers.

    • PreferedCustomers <1-----*> Orders

    • CreditRiskCustomers <1-----*> CreditReports

  • In a one-to-many association, an entity-set on the "one" end can have an association with an entity implemented as MEST on the "many" end. For example, the Product entity can be included in two entity sets and in associations with the ManufacturingUnit entity.

    • ManufacturingUnit <1-----*> Products

    • ManufacturingUnit <1----*> DefectiveProducts

MEST implementation in a “one-one” or a “many-many” scenario will be difficult to implement when designing the logical model and/or when mapping the logical model to the conceptual model. The problem with one-to-one and many-to-many MEST is the same as having MEST on the "one" end with a relationship to the same entity set on the "many" end. With one-to-one, it is possible to create a logical model similar to the cases for one-to-many.

  • The following orders scenario will run into issues when designing the logical model.

    • PreferedCustomers <1-----*> Orders

    • CreditRiskCustomers <1-----*> Orders

For more information, see How to: Define a Model with Multiple Entity Sets per Type (Entity Framework).

See Also

Concepts

Association (EDM)
Association Sets (EDM)
Entity Containers (EDM)
Entity Data Model Types
Simple Types (EDM)

Other Resources

Schemas and Mapping Specification (Entity Framework)