Condividi tramite


NavigationProperty Element (EntityType CSDL)

In the Entity Data Model (EDM), a NavigationProperty element declares a shortcut that is used to navigate to and from entities related by the Association type.

For example, if an Association is defined between an Employee type and another Employee type that is designated as manager, a navigation property can be specified on the Employee entity to navigate from the managed Employee to the manager or from the manager to the managed employee.

The following excerpt from the Human Relations segment of the AdventureWorksHRModel schema defines the Employee entity and the Association between Employee entities that designates some employees as managers. Note that the Association is between entities of the same type but of a different Role.

The Association element declare an association named Employee_Employee_ManagerID. The only distinguishing attribute of the Employee entities at the ends of this association is the Role attribute specified in the AssociationSet definition. The latter Role specifies EmployeeManager.

The Multiplicity attribute of the End elements in the Association declaration specifies that an Employee can be the manager of zero or more employees. An Employee can have only one manager as designated by the Role EmployeeManager.

  <EntityType Name="Employee" Key="EmployeeID">
    <Key>
        <PropertyRef Name="EmployeeID">
    </Key>
    <Property Name="EmployeeID" Type="Int32" Nullable="false" />
    <!-- Other properties omitted for brevity-->

<NavigationProperty Name="Employee1" 
Relationship="Adventureworks.FK_Employee_Employee_ManagerID" 
FromRole="Employee" ToRole="Employee1" />  </EntityType>

<NavigationProperty Name="Employee2" 
Relationship="Adventureworks.FK_Employee_Employee_ManagerID" 
FromRole="Employee1" ToRole="Employee" />

  <EntityContainer Name="HumanResources">

    <EntitySet Name="Employee" EntityType="Adventureworks.Employee" />

    <AssociationSet Name="FK_Employee_Employee_ManagerID"
      Association="Adventureworks.FK_Employee_Employee_ManagerID">
        <End Role="Employee" EntitySet="Employee" />
        <End Role="Employee1" EntitySet="Employee" />
    </AssociationSet>

  </EntityContainer>

The NavigationProperty is a shortcut to the Employee entity that is manager of the Employee entity that contains the NavigationProperty. When the object model based on these specifications is built, the Employee class includes a Manager property and helper methods that are used to navigate the association designated by the NavigationProperty. The helper methods are automatically built with the object model.

Navigation properties can be used to implement properties that function like collections in code. For more information about navigation properties and collections, see Navigation Properties (EDM).

The NavigationProperty element is only used in conceptual schema definition language (CSDL). There is a counterpart in the store schema definition language (SSDL) metadata to specify primary key/foreign key relationships in the database that support this association. In the metadata, this association includes a referential constraint. For more information about the ReferentialConstraint attribute see ReferentialConstraint Element (Association SSDL) and Association Element (SSDL).

See Also

Concepts

Entity Data Model Relationships
Association Element (CSDL)
Multiplicity Attribute (Association CSDL)
AdventureWorks Complete Model (EDM)
Navigation Properties (EDM)
Association Element (SSDL)
ReferentialConstraint Element (Association SSDL)