This documentation is archived and is not being maintained.

EntityReference<TEntity> Class

Represents a related end of an association with a multiplicity of zero or one.

Namespace:  System.Data.Objects.DataClasses
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public sealed class EntityReference<TEntity> : EntityReference 
where TEntity : class, IEntityWithRelationships

Type Parameters


The entity type of the reference.

An EntityReference<TEntity> object is returned by a navigation property when the related end has a multiplicity of zero or one. For more information, see Navigation Properties (EDM).

An EntityReference<TEntity> object might have a corresponding EntityCollection<TEntity> (one-to-many relationship) or EntityReference<TEntity> (one-to-one relationship) at the other end of the relationship. When an EntityReference<TEntity> and an EntityCollection<TEntity> model opposing ends of the same relationship, the integrity of the relationship is maintained at the object level.

This class cannot be inherited.

This example shows how to use the EntityReference<TEntity> object to change a relationship between a SalesOrderHeader object and a related Address object that represents the shipping address for the order.

// Define the order and new address IDs. 
int orderId = 43669;
int newAddressId = 26;

using (AdventureWorksEntities context 
    = new AdventureWorksEntities())
        // Get the billing address to change to.
        Address newAddress = context.Address
            .Where("it.AddressID = @addressId",
            new ObjectParameter("addressId", newAddressId))

        // Get the order being changed.
        SalesOrderHeader order = context.SalesOrderHeader
            .Where("it.SalesOrderID = @orderId",
            new ObjectParameter("orderId", orderId)).First();

        // Load the current billing address. 
        if (!order.Address1Reference.IsLoaded)

        // Write the current billing street address.
        Console.WriteLine("Current street: " 
            + order.Address1.AddressLine1);

        // Change the billing address. 
        if (!order.Address1.Equals(newAddress))
            order.Address1 = newAddress;

            // Write the changed billing street address.
            Console.WriteLine("Changed street: "
                + order.Address1.AddressLine1);

        // If the address change succeeds, save the changes.

        // Write the current billing street address.
        Console.WriteLine("Current street: "
            + order.Address1.AddressLine1);
    catch (ApplicationException ex)
        // Handle the exception raised in the ShippingAddress_Changed  
        // handler when the status of the order prevents the  
        // shipping address from being changed. Don't retry because 
        // the relationship is in an inconsistent state and calling  
        // SaveChanges() will result in an UpdateException.
    catch (InvalidOperationException ex)

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1