.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<DataContractAttribute> _
Public NotInheritable Class EntityReference(Of TEntity As {Class, IEntityWithRelationships}) _
    Inherits EntityReference
Visual Basic (Usage)
Dim instance As EntityReference(Of TEntity)
C#
[SerializableAttribute]
[DataContractAttribute]
public sealed class EntityReference<TEntity> : EntityReference
where TEntity : class, IEntityWithRelationships
Visual C++
[SerializableAttribute]
[DataContractAttribute]
generic<typename TEntity>
where TEntity : ref class, IEntityWithRelationships
public ref class EntityReference sealed : public EntityReference
JScript
JScript does not support generic types or methods.

Type Parameters

TEntity

The entity type of the reference.

Remarks

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.

Examples

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.

Visual Basic
'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
C#
// Define the order and new address IDs.
int orderId = 43669;
int newAddressId = 26;

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

        // 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)
        {
            order.Address1Reference.Load();
        }

        // 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.
        context.SaveChanges();

        // 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.
        Console.WriteLine(ex.ToString());
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}
Inheritance Hierarchy

System..::.Object
  System.Data.Objects.DataClasses..::.RelatedEnd
    System.Data.Objects.DataClasses..::.EntityReference
      System.Data.Objects.DataClasses..::.EntityReference<(Of <(TEntity>)>)
Thread Safety

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

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.
Version Information

.NET Framework

Supported in: 3.5 SP1
See Also

Reference

Other Resources

Tags :


Page view tracker