EntityReference(TEntity) Class

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)

System.Object
  System.Data.Objects.DataClasses.RelatedEnd
    System.Data.Objects.DataClasses.EntityReference
      System.Data.Objects.DataClasses.EntityReference<TEntity>

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

Type Parameters

TEntity

The entity type of the reference.

NameDescription
System_CAPS_pubmethodEntityReference<TEntity>()

Creates a new instance of EntityReference<TEntity>.

NameDescription
System_CAPS_pubpropertyEntityKey

Returns the key for the related object. (Inherited from EntityReference.)

System_CAPS_pubpropertyIsLoaded

Gets a value that indicates whether all related objects have been loaded.(Inherited from RelatedEnd.)

System_CAPS_pubpropertyRelationshipName

Gets the name of the relationship in which this related end participates.(Inherited from RelatedEnd.)

System_CAPS_pubpropertyRelationshipSet

Gets a reference to the metadata for the related end.(Inherited from RelatedEnd.)

System_CAPS_pubpropertySourceRoleName

Gets the role name at the source end of the relationship.(Inherited from RelatedEnd.)

System_CAPS_pubpropertyTargetRoleName

Gets the role name at the target end of the relationship.(Inherited from RelatedEnd.)

System_CAPS_pubpropertyValue

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

NameDescription
System_CAPS_pubmethodAttach(TEntity)

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

System_CAPS_pubmethodCreateSourceQuery()

Creates an equivalent object query that returns the related object.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an IEnumerator that iterates through the collection of related objects.(Inherited from RelatedEnd.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodLoad()

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

System_CAPS_pubmethodLoad(MergeOption)

Loads the related object for this EntityReference<TEntity> with the specified merge option.(Overrides RelatedEnd.Load(MergeOption).)

System_CAPS_pubmethodOnDeserialized(StreamingContext)

This API supports the product infrastructure and is not intended to be used directly from your code. Used internally to deserialize entity objects along with the RelationshipManager instances.(Inherited from RelatedEnd.)

System_CAPS_pubmethodOnRefDeserialized(StreamingContext)

This API supports the product infrastructure and is not intended to be used directly from your code. This method is used internally to serialize related entity objects.

System_CAPS_pubmethodOnSerializing(StreamingContext)

This API supports the product infrastructure and is not intended to be used directly from your code. This method is used internally to serialize related entity objects.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubeventAssociationChanged

Occurs when a change is made to a related end.(Inherited from RelatedEnd.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.Add(IEntityWithRelationships)

Adds an object to the related end.(Inherited from RelatedEnd.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.Add(Object)

Adds an object to the related end.(Inherited from RelatedEnd.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.Attach(IEntityWithRelationships)

Attaches an object at the related end.(Inherited from RelatedEnd.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.Attach(Object)

Attaches an object to the related end.(Inherited from RelatedEnd.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.CreateSourceQuery()

Returns an IEnumerable that represents the objects that belong to the related end.(Inherited from RelatedEnd.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.Remove(IEntityWithRelationships)

Removes an object from the collection at the related end.(Inherited from RelatedEnd.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodIRelatedEnd.Remove(Object)

Removes an object from the related end if the object is part of the related end.(Inherited from RelatedEnd.)

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.

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
Available since 3.5

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

Return to top
Show:
© 2016 Microsoft