This documentation is archived and is not being maintained.

EntityReference(Of 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)

'Declaration
<SerializableAttribute> _
<DataContractAttribute> _
Public NotInheritable Class EntityReference(Of TEntity As {Class, IEntityWithRelationships}) _
	Inherits EntityReference
'Usage
Dim instance As EntityReference(Of TEntity)

Type Parameters

TEntity

The entity type of the reference.

An EntityReference(Of 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(Of TEntity) object might have a corresponding EntityCollection(Of TEntity) (one-to-many relationship) or EntityReference(Of TEntity) (one-to-one relationship) at the other end of the relationship. When an EntityReference(Of TEntity) and an EntityCollection(Of 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(Of 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. 
Dim orderId As Integer = 43669
Dim newAddressId As Integer = 26

Using context As New AdventureWorksEntities()
    Try 
        ' Get the billing address to change to. 
        Dim newAddress As Address = context.Address _
            .Where("it.AddressID = @addressId", _
            New ObjectParameter("addressId", newAddressId)) _
        .First()

        ' Get the order being changed. 
        Dim order As SalesOrderHeader = context.SalesOrderHeader _
            .Where("it.SalesOrderID = @orderId", _
            New ObjectParameter("orderId", orderId)).First()

        ' Load the current billing address. 
        If Not order.Address1Reference.IsLoaded Then
            order.Address1Reference.Load()
        End If 

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

        ' Change the billing address. 
        If Not order.Address1.Equals(newAddress) Then
            order.Address1 = newAddress

            ' Write the changed billing street address.
            Console.WriteLine("Changed street: " _
                + order.Address1.AddressLine1)
        End If 

        ' If the address change succeeds, save the changes.
        context.SaveChanges()

        ' Write the current billing street address.
        Console.WriteLine("Current street: " _
            + order.Address1.AddressLine1)
    Catch ex As ApplicationException
        ' 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.
        Console.WriteLine(ex.ToString())
    Catch ex As InvalidOperationException
        Console.WriteLine(ex.ToString())
    End Try 
End Using

System.Object
  System.Data.Objects.DataClasses.RelatedEnd
    System.Data.Objects.DataClasses.EntityReference
      System.Data.Objects.DataClasses.EntityReference(Of TEntity)

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