Partilhar via


Association Element (CSDL)

An Association element defines a relationship between two entity types. An association must specify the entity types that are involved in the relationship and the possible number of entity types at each end of the relationship, which is known as the multiplicity. The multiplicity of an association end can have a value of one (1), zero or one (0..1), or many (*). This information is specified in two child End elements.

Entity type instances at one end of an association can be accessed through navigation properties or foreign keys, if they are exposed on an entity type. For more information, see NavigationProperty element, ReferentialConstraint element, and Defining and Managing Relationships.

In an application, an instance of an association represents a specific association between instances of entity types. Association instances are logically grouped in an association set. For more information, see AssociationSet element.

An Association element can have the following child elements (in the order listed):

Applicable Attributes

The table below describes the attributes that can be applied to the Association element.

 

Attribute Name Is Required Value

Name

Yes

The name of the association.

Note

Any number of annotation attributes (custom XML attributes) may be applied to the Association element. However, custom attributes may not belong to any XML namespace that is reserved for CSDL. The fully-qualified names for any two custom attributes cannot be the same.

Example

The following example shows an Association element that defines the CustomerOrders association when foreign keys have not been exposed on the Customer and Order entity types. The Multiplicity values for each End of the association indicate that many Orders can be associated with a Customer , but only one Customer can be associated with an Order . Additionally, the OnDelete element indicates that all Orders that are related to a particular Customer and have been loaded into the ObjectContext will be deleted if the Customer is deleted.

<Association Name="CustomerOrders">
  <End Type="ExampleModel.Customer" Role="Customer" Multiplicity="1" >
        <OnDelete Action="Cascade" />
  </End>
  <End Type="ExampleModel.Order" Role="Order" Multiplicity="*" />
</Association>

The following example shows an Association element that defines the CustomerOrders association when foreign keys have been exposed on the Customer and Order entity types. With foreign keys exposed, the relationship between the entities is managed with a ReferentialConstraint element. A corresponding AssociationSetMapping element is not necessary to map this association to the data source.

<Association Name="CustomerOrders">
  <End Type="ExampleModel.Customer" Role="Customer" Multiplicity="1" >
        <OnDelete Action="Cascade" />
  </End>
  <End Type="ExampleModel.Order" Role="Order" Multiplicity="*" />
  <ReferentialConstraint>
       <Principal Role="Customer">
           <PropertyRef Name="Id" />
       </Principal>
       <Dependent Role="Order">
            <PropertyRef Name="CustomerId" />
        </Dependent>
  </ReferentialConstraint>
</Association>

See Also

Concepts

Entity Framework Overview
CSDL Specification
Schema Element (CSDL)

Other Resources

CSDL, SSDL, and MSL Specifications
ADO.NET Entity Data Model Tools
association type (Entity Data Model)

Build Date:

2015-07-20