共用方式為


NavigationProperty 項目(EntityType CSDL)

在 實體資料模型 (EDM) 中,NavigationProperty 項目會宣告一個捷徑,此捷徑是用於來回巡覽以 Association 型別產生關聯的實體。

例如,如果在 Employee 型別以及指定為經理的另一個 Employee 型別之間定義了 Association,就可以在 Employee 實體上指定導覽屬性,以便從受管理的 Employee 巡覽至經理,或是從經理巡覽至受管理的員工。

下列來自 AdventureWorksHRModel 結構描述之 Human Relations 區段中的節錄會定義 Employee 實體以及指定某些員工為經理之 Employee 實體之間的 Association。請注意,Association 是在相同型別的實體之間,但是具有不同的 Role

Association 項目會宣告名為 Employee_Employee_ManagerID 的關聯。唯一可區分此關聯兩端之 Employee 實體的屬性是 AssociationSet 定義內所指定的 Role 屬性。後者的 Role 會指定 EmployeeManager

Association 宣告中 End 項目的 Multiplicity 屬性指定 Employee 可為一或零個員工的經理。Employee 只能有一個經理,如 RoleEmployeeManager 所指定。

  <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>

NavigationPropertyEmployee 實體的捷徑,該實體是包含 NavigationPropertyEmployee 實體的經理。當建置根據這些規格的物件模型時,Employee 類別包含 Manager 屬性及 Helper 方法,這些方法可用來巡覽 NavigationProperty 所指定的關聯。Helper 方法會使用物件模型自動建置。

可使用導覽屬性來實作一些屬性,這些屬性在程式碼中的作用就像是集合。如需導覽屬性和集合的詳細資訊,請參閱導覽屬性 (EDM)

NavigationProperty 項目只會在概念結構定義語言 (CSDL) 中使用。存放結構定義語言 (SSDL) 中繼資料內有一個類似項目,可在資料庫中指定支援這個關聯的主索引鍵/外部索引鍵關聯性。在中繼資料內,這個關聯包含參考條件約束。如需 ReferentialConstraint 屬性的詳細資訊,請參閱 ReferentialConstraint 項目 (關聯 SSDL)Association 項目 (SSDL)

另請參閱

概念

Entity Data Model 關聯性
Association 項目 (CSDL)
Multiplicity 屬性 (關聯 CSDL)
AdventureWorks 完整模型 (EDM)
導覽屬性 (EDM)
Association 項目 (SSDL)
ReferentialConstraint 項目 (關聯 SSDL)