共用方式為


Association (EDM)

In the Entity Data Model (EDM), relationships model logical connections between entities. The EDM supports the AssociationType of relationship, which models the peer-to-peer relationship between entities.

In an association, each participating entity is called an End. Each End has a Role attribute that is used to name and describe the logic of each End of the association.

The Type attributes on the ends of an association define the entity types participating in the relationship.

Associations have a Multiplicity attribute. The Multiplicity attribute specifies the number of instances of each End that can participate in the relationship.

In the logic of many LOB applications, customers make orders and orders must be delivered to customers. The relationship between a customer and an order can be modeled by an EDM association. The ends of the association are the customer and order entities. Each End has an entity Type. The Role attribute describes the function of the entity specified by the Type attribute. In most cases, each customer can have zero or more orders, but an order is associated with exactly one customer. In other words, the Multiplicity of the customer type is 1, and the Multiplicity of order is *. For more information about Multiplicity, see Entity Data Model Relationships.

Example

In the following conceptual schema definition language (CSDL) syntax, an Association element specifies the logic of the relationship between Customers and Orders.

<?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>
      <PropertyRef Name="CustomerId" />
    </Key>
    <Property Name="CustomerId" Type="Guid" Nullable="false" />
    <Property Name="Name" Type="String" Nullable="false" />
    <Property Name="Address" Type="String" Nullable="false" />
    <Property Name="City" Type="String" Nullable="false" />
    <NavigationProperty Name="Orders" Relationship="Self.Order_Customer" FromRole="Customers" ToRole="Orders" />
  </EntityType>

  <EntityType Name="Order">
    <Key>
      <PropertyRef Name="OrderId" />
    </Key>
    <Property Name="OrderId" Type="String" Nullable="false" />
    <Property Name="TotalAmount" Type="Decimal" />
    <Property Name="Tax" Type="Decimal" />
    <Property Name="ShippingAddress" Type="String" />
    <NavigationProperty Name="Customer" Relationship="Self.Order_Customer" FromRole="Orders" ToRole="Customers" />
  </EntityType>

  <Association Name="Order_Customer">
    <End Role="Customer" Type="OrderInfoModel.Customers"
                             Multiplicity="1" />
    <End Role="Orders" Type="OrderInfoModel.Order" Multiplicity="*" />
  </Association>


    <EntityContainer Name="LOBSchemaData">
        <EntitySet Name="Customers" EntityType="Self.Customer" />
        <EntitySet Name="Orders" EntityType="Self.Order" />
        <AssociationSet Name="Customers_Orders"
                        Association="Self.Order_Customer">
            <End Role="Customer" EntitySet="Customers" />
            <End Role="Order" EntitySet="Orders" />
        </AssociationSet>
        
    </EntityContainer>
</Schema>

The Name attribute of the Order_Customer Association is used to identify the relationship in all operations. Each participating entity is an End. The Type attribute of an End element identifies the entity type of the End element. Each participating entity is an End specifies the entity types participating in the association, in this case, Customers and Orders. Each End has a Role attribute and a Multiplicity attribute.

Features of EDM Associations

An association has the following characteristics:

  • Multiplicity: Association End elements can be 0..n, where the cardinality of either End can vary.

  • Non-Exclusive Membership: An entity type can participate in multiple associations.

  • Direction: Associations are bi-directional between entities and can be navigated from either End.

Multiplicity

The Multiplicity attribute defines the cardinality of the Association in all operations. Each participating entity is an End elements of an Association. The previous CSDL example specifies that there is exactly one Customer (Multiplicty="1") for zero or more orders (Multiplicty="*").

The following table lists all the possible values for Multiplicity:

Value Description

0..1

Zero or one

1

Exactly one

*

Zero or more

Non-Exclusive Membership

An entity type can participate in multiple associations.

Direction

Associations in the EDM are bi-directional.

See Also

Concepts

Entity Data Model Relationships