Partilhar via


NavigationProperty Element (CSDL)

A NavigationProperty element defines a navigation property, which provides a reference to the other end of an association. Unlike properties defined with the Property element, navigation properties do not define the shape and characteristics of data. They provide a way to navigate an association between two entity types.

Note that navigation properties are optional on both entity types at the ends of an association. If you define a navigation property on one entity type at the end of an association, you do not have to define a navigation property on the entity type at the other end of the association.

The data type returned by a navigation property is determined by the multiplicity of its remote association end. For example, suppose a navigation property, OrdersNavProp, exists on a Customer entity type and navigates a one-to-many association between Customer and Order. Because the remote association end for the navigation property has multiplicity many (*), its data type is a collection (of Order). Similarly, if a navigation property, CustomerNavProp, exists on the Order entity type, its data type would be Customer since the multiplicity of the remote end is one (1).

A NavigationProperty 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 NavigationProperty element.

Attribute Name Is Required Value

Name

Yes

The name of the navigation property.

Relationship

Yes

The name of an association that is within the scope of the model.

ToRole

Yes

The end of the association at which navigation ends. The value of the ToRole attribute must be the same as the value of one of the Role attributes defined on one of the association ends (defined in the AssociationEnd element).

FromRole

Yes

The end of the association from which navigation begins. The value of the FromRole attribute must be the same as the value of one of the Role attributes defined on one of the association ends (defined in the AssociationEnd element).

Note

Any number of annotation attributes (custom XML attributes) may be applied to the NavigationProperty 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 defines an entity type (Book) with two navigation properties (PublishedBy and WrittenBy):

<EntityType Name="Book">
  <Key>
    <PropertyRef Name="ISBN" />
  </Key>
  <Property Type="String" Name="ISBN" Nullable="false" />
  <Property Type="String" Name="Title" Nullable="false" />
  <Property Type="Decimal" Name="Revision" Nullable="false" Precision="29" Scale="29" />
  <NavigationProperty Name="Publisher" Relationship="BooksModel.PublishedBy"
                      FromRole="Book" ToRole="Publisher" />
  <NavigationProperty Name="Authors" Relationship="BooksModel.WrittenBy"
                      FromRole="Book" ToRole="Author" />
</EntityType>

See Also

Concepts

Entity Framework Overview
CSDL Specification
Schema Element (CSDL)

Other Resources

CSDL, SSDL, and MSL Specifications
ADO.NET Entity Data Model Tools
navigation property (Entity Data Model)