EntityReference<TEntity> EntityReference<TEntity> EntityReference<TEntity> EntityReference<TEntity> Class

Definition

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

generic <typename TEntity>
 where TEntity : classpublic ref class EntityReference sealed : System::Data::Objects::DataClasses::EntityReference
[System.Runtime.Serialization.DataContract]
[System.Serializable]
public sealed class EntityReference<TEntity> : System.Data.Objects.DataClasses.EntityReference where TEntity : class
type EntityReference<'Entity (requires 'Entity : null)> = class
    inherit EntityReference
Public NotInheritable Class EntityReference(Of TEntity)
Inherits EntityReference

Type Parameters

TEntity

The entity type of the reference.

Inheritance
EntityReference<TEntity>EntityReference<TEntity>EntityReference<TEntity>EntityReference<TEntity>
Attributes

Examples

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 addressId = 26;

using (AdventureWorksEntities context
    = new AdventureWorksEntities())
{
    // Get the billing address to change to.
    Address address =
        context.Addresses.Single(c => c.AddressID == addressId);

    // Get the order being changed.
    SalesOrderHeader order =
        context.SalesOrderHeaders.Single(o => o.SalesOrderID == orderId);

    // You do not have to call the Load method to load the addresses for the order,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case Address.

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

    // Change the billing address.
    if (!order.Address.Equals(address))
    {
        // Use Address navigation property to change the association.
        order.Address = address;

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

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

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

' Define the order and new address IDs. 
Dim orderId As Integer = 43669
Dim addressId As Integer = 26

Using context As New AdventureWorksEntities()
    ' Get the billing address to change to. 
    Dim address As Address = context.Addresses.Single(Function(c) c.AddressID = addressId)

    ' Get the order being changed. 
    Dim order As SalesOrderHeader = context.SalesOrderHeaders.Single(Function(o) o.SalesOrderID = orderId)

    ' You do not have to call the Load method to load the addresses for the order, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case Address. 

    ' Write the current billing street address. 
    Console.WriteLine("Current street: " & order.Address.AddressLine1)

    ' Change the billing address. 
    If Not order.Address.Equals(address) Then
        ' Use Address navigation property to change the association. 
        order.Address = address

        ' Write the changed billing street address. 
        Console.WriteLine("Changed street: " & order.Address.AddressLine1)
    End If

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

    ' Write the current billing street address. 
    Console.WriteLine("Current street: " & order.Address.AddressLine1)
End Using

Remarks

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 NavigationProperty Element (CSDL).

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.

Constructors

EntityReference<TEntity>() EntityReference<TEntity>() EntityReference<TEntity>() EntityReference<TEntity>()

Creates a new instance of EntityReference<TEntity>.

Properties

EntityKey EntityKey EntityKey EntityKey

Returns the key for the related object.

(Inherited from EntityReference)
IsLoaded IsLoaded IsLoaded IsLoaded

Gets a value that indicates whether all related objects have been loaded.

(Inherited from RelatedEnd)
RelationshipName RelationshipName RelationshipName RelationshipName

Gets the name of the relationship in which this related end participates.

(Inherited from RelatedEnd)
RelationshipSet RelationshipSet RelationshipSet RelationshipSet

Gets a reference to the metadata for the related end.

(Inherited from RelatedEnd)
SourceRoleName SourceRoleName SourceRoleName SourceRoleName

Gets the role name at the source end of the relationship.

(Inherited from RelatedEnd)
TargetRoleName TargetRoleName TargetRoleName TargetRoleName

Gets the role name at the target end of the relationship.

(Inherited from RelatedEnd)
Value Value Value Value

Gets or sets the related object returned by this EntityReference<TEntity>.

Methods

Attach(TEntity) Attach(TEntity) Attach(TEntity) Attach(TEntity)

Creates a many-to-one or one-to-one relationship between two objects in the object context.

Attach<TEntity>(IEnumerable<TEntity>, Boolean) Attach<TEntity>(IEnumerable<TEntity>, Boolean) Attach<TEntity>(IEnumerable<TEntity>, Boolean) Attach<TEntity>(IEnumerable<TEntity>, Boolean)

Defines a relationship between two attached objects.

(Inherited from RelatedEnd)
CreateSourceQuery() CreateSourceQuery() CreateSourceQuery() CreateSourceQuery()

Creates an equivalent object query that returns the related object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

Returns an IEnumerator that iterates through the collection of related objects.

(Inherited from RelatedEnd)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Load() Load() Load() Load()

When overridden in a derived class, loads the related object or objects into the related end with the default merge option.

(Inherited from RelatedEnd)
Load(MergeOption) Load(MergeOption) Load(MergeOption) Load(MergeOption)

Loads the related object for this EntityReference<TEntity> with the specified merge option.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean) Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean) Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean) Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean)

Merges related entities into the local EntityCollection<TEntity>.

(Inherited from RelatedEnd)
OnDeserialized(StreamingContext) OnDeserialized(StreamingContext) OnDeserialized(StreamingContext) OnDeserialized(StreamingContext)

Used internally to deserialize entity objects along with the RelationshipManager instances.

(Inherited from RelatedEnd)
OnRefDeserialized(StreamingContext) OnRefDeserialized(StreamingContext) OnRefDeserialized(StreamingContext) OnRefDeserialized(StreamingContext)

This method is used internally to serialize related entity objects.

OnSerializing(StreamingContext) OnSerializing(StreamingContext) OnSerializing(StreamingContext) OnSerializing(StreamingContext)

This method is used internally to serialize related entity objects.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean) ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean) ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean) ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean)

Determines whether an object can be attached to the local collection or reference.

(Inherited from RelatedEnd)
ValidateLoad<TEntity>(MergeOption, String) ValidateLoad<TEntity>(MergeOption, String) ValidateLoad<TEntity>(MergeOption, String) ValidateLoad<TEntity>(MergeOption, String)

Ensures that the related object or objects can be successfully loaded into the local collection or reference.

(Inherited from RelatedEnd)
ValidateOwnerForAttach() ValidateOwnerForAttach() ValidateOwnerForAttach() ValidateOwnerForAttach()

Ensures that the object to which the related end belongs supports an attach operation.

(Inherited from RelatedEnd)

Events

AssociationChanged AssociationChanged AssociationChanged AssociationChanged

Occurs when a change is made to a related end.

(Inherited from RelatedEnd)

Explicit Interface Implementations

IRelatedEnd.Add(IEntityWithRelationships) IRelatedEnd.Add(IEntityWithRelationships) IRelatedEnd.Add(IEntityWithRelationships) IRelatedEnd.Add(IEntityWithRelationships)

Adds an object to the related end.

(Inherited from RelatedEnd)
IRelatedEnd.Add(Object) IRelatedEnd.Add(Object) IRelatedEnd.Add(Object) IRelatedEnd.Add(Object)

Adds an object to the related end.

(Inherited from RelatedEnd)
IRelatedEnd.Attach(IEntityWithRelationships) IRelatedEnd.Attach(IEntityWithRelationships) IRelatedEnd.Attach(IEntityWithRelationships) IRelatedEnd.Attach(IEntityWithRelationships)

Attaches an object at the related end.

(Inherited from RelatedEnd)
IRelatedEnd.Attach(Object) IRelatedEnd.Attach(Object) IRelatedEnd.Attach(Object) IRelatedEnd.Attach(Object)

Attaches an object to the related end.

(Inherited from RelatedEnd)
IRelatedEnd.CreateSourceQuery() IRelatedEnd.CreateSourceQuery() IRelatedEnd.CreateSourceQuery() IRelatedEnd.CreateSourceQuery()

Returns an IEnumerable that represents the objects that belong to the related end.

(Inherited from RelatedEnd)
IRelatedEnd.Remove(IEntityWithRelationships) IRelatedEnd.Remove(IEntityWithRelationships) IRelatedEnd.Remove(IEntityWithRelationships) IRelatedEnd.Remove(IEntityWithRelationships)

Removes an object from the collection at the related end.

(Inherited from RelatedEnd)
IRelatedEnd.Remove(Object) IRelatedEnd.Remove(Object) IRelatedEnd.Remove(Object) IRelatedEnd.Remove(Object)

Removes an object from the related end if the object is part of the related end.

(Inherited from RelatedEnd)

Applies to