Share via

Navigation Properties

Navigation properties in the Entity Framework provide a way to navigate an association between two entity types. A navigation property is defined by the NavigationProperty Element (CSDL) in the conceptual model. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either an EntityReference, if the multiplicity is either one or zero-or-one, or an EntityCollection, if the multiplicity is many. You may also choose to have one-way navigation, in which case you can delete the navigation property, for more information, see How to: Edit and Delete Navigation Properties

When you use the Entity Framework-generated classes, navigation properties are created for objects that participate in a relationship. For information on working with navigation properties in POCO entities, see Requirements for Creating POCO Proxies.

Modifying Relationships

You can use navigation properties to change a relationship. The following example assigns a different customer to an order by using the Customer navigation property on the Order object to access the customer reference that is associated with that order: order.Customer = customer.

The following example adds an order to an existing customer order by using the Orders navigation property to access the collection of orders that belong to the customer:


In a foreign key association, you can form or change a relationship by setting the foreign key property of the dependent object, as in the following example:

order.CustomerID = CustomerID.

For more information about independent and foreign key associations, see Defining and Managing Relationships.

You can use navigation properties to load objects that are related to an entity by the defined association. For more information, see Loading Related Objects and How to: Navigate Relationships Using Navigation Properties.

The following example in method-based query syntax uses the SelectMany method to get all the orders of the contacts whose last name is "Zhou". The Contact.SalesOrderHeader navigation property is used to get the collection of SalesOrderHeader objects for each contact.

Dim lastName = "Zhou"
Using context As New AdventureWorksEntities
    Dim ordersQuery = context.Contacts _
    .Where(Function(c) c.LastName = lastName) _
    .SelectMany(Function(o) o.SalesOrderHeaders)

    For Each order In ordersQuery
        Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
                order.SalesOrderID, order.OrderDate, order.TotalDue)
End Using
string lastName = "Zhou";
using (AdventureWorksEntities context = new AdventureWorksEntities())
    IQueryable<SalesOrderHeader> ordersQuery = context.Contacts
        .Where(c => c.LastName == lastName)
        .SelectMany(c => c.SalesOrderHeaders);

    foreach (var order in ordersQuery)
        Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}",
            order.SalesOrderID, order.OrderDate, order.TotalDue);

See Also


Defining and Managing Relationships