Compartilhar via


Association Sets (EDM)

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

The designation AssociationSet is used for two reasons:

  • To scope the endpoints of an Association.

  • To manage associations between entity instances.

Because an EntityType can be used by more than one EntitySet, association sets are needed to scope the endpoints of the relationship.

An Association connects two or more entity instances that belong to the two entity sets specified by the Association. An AssociationSet contains instances, if any, of the Association.

Entity types are logically contained and instantiated in entity sets and in entity containers. In the same way, associations are instantiated in association sets and in entity containers.

Associations and association sets are abstract concepts, just like entities and entity sets. Each implementation of an AssociationSet is a derived type.

The Association attribute of an AssociationSet element specifies an Association in the following conceptual schema definition language (CSDL) schema syntax:

<AssociationSet Name="CustomerOrderSet" Association="CustomerOrderType">

Two ends of EntitySet are specified in the End properties of the AssociationSet. The association must be fully qualified by the namespace name.

<AssociationSet Name="CustomerOrderSet" Association=" 
                        MyCompany.LOBSchema.CustomerOrderType">
    <End Role="Orders" EntitySet="CustomerSet" />
    <End Role="OrderedBy" EntitySet="OrderSet" />
</AssociationSet >

The End properties of the AssociationSet specify the EntitySet instances that correspond to these EntityType instances.

The following example shows declarations of two entity types, two entity sets, an association and an association set:

<?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="Customer">
        <Key>
          <Property Name="CustomerId" Type="Int32" Nullable="false" />
        </Key>
    <!-- Other properties -->
    </EntityType>


<EntityType Name="Order">
    <Key>
      <PropertyRef Name="OrderId" />
    </Key>
    <Property Name="OrderId" Type="Int32" Nullable="false" />
    <!-- Other properties -->
</EntityType>

<Association Name="CustomerOrderType">
    <End Role="Orders" Type=" Self.Customer" Multiplicity="1" />
    <End Role="OrderedBy" Type=" Self.Order" Multiplicity="0..*" />
</Association>

<EntityContainer Name="ContainerType">
    <EntitySet Name="CustomerSet" EntityType=" Self.Customer" />
    <EntitySet Name="OrderSet" EntityType=" Self.Order" />
    <AssociationSet Name="CustomerOrderSet" Association=" Self.CustomerOrderType">
        <End Role="Orders" EntitySet=" Self.CustomerSet" />
        <End Role="OrderedBy" EntitySet=" Self.OrderSet" />
    </AssociationSet>
</EntityContainer>

</Schema>

This example first defines Customer and Order entities of EntityType. Next, it defines the Association named CustomerOrderType. The entity sets CustomerSet and OrderSet are declared inside the EntityContainer. For more information about entity containers see Entity Containers (EDM).

The AssociationSet element is also defined inside the EntityContainer as an AssociationSet named CustomerOrderSet of type CustomerOrderType.

The two End properties of CustomerOrderSet are EntitySet types, in this case CustomerSet and OrderSet. The Association instances are located in CustomerOrderSet and connect Customer instances in CustomerSet with Order instances in OrderSet.

See Also

Concepts

Association (EDM)
Entity Data Model Relationships
Entity Data Model Types
Entity Sets (EDM)
Entity Containers (EDM)
Schemas (EDM)

Other Resources

Schemas and Mapping Specification (Entity Framework)