Export (0) Print
Expand All

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)

[SerializableAttribute]
[DataContractAttribute]
public sealed class EntityReference<TEntity> : EntityReference 
where TEntity : class

Type Parameters

TEntity

The entity type of the reference.

The EntityReference<TEntity> type exposes the following members.

  NameDescription
Public methodEntityReference<TEntity>Creates a new instance of EntityReference<TEntity>.
Top

  NameDescription
Public propertyEntityKeyReturns the key for the related object. (Inherited from EntityReference.)
Public propertyIsLoadedGets a value that indicates whether all related objects have been loaded. (Inherited from RelatedEnd.)
Public propertyRelationshipNameGets the name of the relationship in which this related end participates. (Inherited from RelatedEnd.)
Public propertyRelationshipSetGets a reference to the metadata for the related end. (Inherited from RelatedEnd.)
Public propertySourceRoleNameGets the role name at the source end of the relationship. (Inherited from RelatedEnd.)
Public propertyTargetRoleNameGets the role name at the target end of the relationship. (Inherited from RelatedEnd.)
Public propertyValueGets or sets the related object returned by this EntityReference<TEntity>.
Top

  NameDescription
Public methodAttachCreates a many-to-one or one-to-one relationship between two objects in the object context.
Public methodCreateSourceQueryCreates an equivalent object query that returns the related object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetEnumeratorReturns an IEnumerator that iterates through the collection of related objects. (Inherited from RelatedEnd.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodLoad()When overridden in a derived class, loads the related object or objects into the related end with the default merge option. (Inherited from RelatedEnd.)
Public methodLoad(MergeOption)Loads the related object for this EntityReference<TEntity> with the specified merge option. (Overrides RelatedEnd.Load(MergeOption).)
Public methodOnDeserializedInfrastructure. Used internally to deserialize entity objects along with the RelationshipManager instances. (Inherited from RelatedEnd.)
Public methodOnRefDeserializedInfrastructure. This method is used internally to serialize related entity objects.
Public methodOnSerializingInfrastructure. This method is used internally to serialize related entity objects.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventAssociationChangedOccurs when a change is made to a related end. (Inherited from RelatedEnd.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIRelatedEnd.Add(IEntityWithRelationships)Adds an object to the related end. (Inherited from RelatedEnd.)
Explicit interface implemetationPrivate methodIRelatedEnd.Add(Object)Adds an object to the related end. (Inherited from RelatedEnd.)
Explicit interface implemetationPrivate methodIRelatedEnd.Attach(IEntityWithRelationships)Attaches an object at the related end. (Inherited from RelatedEnd.)
Explicit interface implemetationPrivate methodIRelatedEnd.Attach(Object)Attaches an object to the related end. (Inherited from RelatedEnd.)
Explicit interface implemetationPrivate methodIRelatedEnd.CreateSourceQueryReturns an IEnumerable that represents the objects that belong to the related end. (Inherited from RelatedEnd.)
Explicit interface implemetationPrivate methodIRelatedEnd.Remove(IEntityWithRelationships)Removes an object from the collection at the related end. (Inherited from RelatedEnd.)
Explicit interface implemetationPrivate methodIRelatedEnd.Remove(Object)Removes an object from the related end if the object is part of the related end. (Inherited from RelatedEnd.)
Top

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 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);
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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