共用方式為


Association Element (SSDL)

In the Entity Data Model (EDM), an association in the metadata schema specifies a logical relationship between entity types defined in store scheme definition language (SSDL).

SSDL syntax for an association declared in the metadata schema resembles that used in the conceptual schema definition language (CSDL), but SSDL syntax includes attributes specifying FromRole and ToRole. These attributes are equivalent to the primary key and foreign key in database tables.

The following schema example includes the Schema element with the Namespace and Alias of the Adventure Works Human Resources target database partition being defined. One EntityType definition is all that is needed in the example because both ends of the Employee_Employee_ManagerID association are Employee entities.

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

  <EntityType Name="Employee">
    <Key>
      <PropertyRef Name="EmployeeID" />
    </Key>
    <Property Name="EmployeeID" Type="int" Nullable="false" />
    <Property Name="NationalIDNumber" Type="nvarchar" Nullable="false" MaxLength="15" />
    <Property Name="ContactID" Type="int" Nullable="false" />
    <Property Name="LoginID" Type="nvarchar" Nullable="false" MaxLength="256" />
    <Property Name="ManagerID" Type="int" />
<!- Some employee properties ommitted for brevity -->
  </EntityType>

  <Association Name="FK_Employee_Employee_ManagerID">
    <End Role="Employee" Type="Adventureworks.Store.Employee" Multiplicity="0..1" />
    <End Role="Employee1" Type="Adventureworks.Store.Employee" Multiplicity="*" />
    <ReferentialConstraint>
      <Principal Role="Employee">
        <PropertyRef Name="EmployeeID" />
      </Principal>
      <Dependent Role="Employee1">
        <PropertyRef Name="ManagerID" />
      </Dependent>
    </ReferentialConstraint>
  </Association>

In this declaration of the Employee entity used in the association Employee_Employee_ManagerID, the properties of the Employee entity are designated using data types of the storage model, in this case SQL Server types.

The Association element defines a logical relationship between two Employee entities. The Type assignments of the End properties of the Association are both Employee entities. One of the employees is designated as Employee1 by the Role attribute of an End property of the Association.

Type assignments in associations must use fully qualified names. In this example, the type assignment uses the Self Alias for the AdventureWorksHRTarget namespace.

The Mulitiplicity of the End attributes of this Association indicates that an Employeee1 can participate in zero or more instances of this association; the manager can manage several employees, but a managed Employee1 can only report to one manager and therefore can participate in zero or one association of this type.

A ReferentialConstraint on the Association specifies Employee1 as the DependentRole and the Employee as the PrincipalRole. This constraint corresponds to the foreign key ManagerID in the Employee data table.

For more information about the attributes of associations, see the following subtopics:

Name (Association SSDL)

Role Attribute (Association SSDL)

Type Attribute (Association SSDL)

Multiplicity Attribute (Association SSDL)

ReferentialConstraint Element (Association SSDL)

See Also

Concepts

AssociationSet Element (EntityContainer SSDL)
Association Element (CSDL)
AssociationSetMapping Element (MSL)
ReferentialConstraint Element (Association SSDL)
AdventureWorks Complete Model (EDM)