EntityReference<TEntity> 類別

定義

表示多重性為零或一之關聯的相關端。Represents a related end of an association with a multiplicity of zero or one.

generic <typename TEntity>
 where TEntity : class, IEntityWithRelationshipspublic ref class EntityReference sealed : System::Data::Objects::DataClasses::EntityReference
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, IEntityWithRelationships
[System.Runtime.Serialization.DataContract]
[System.Serializable]
public sealed class EntityReference<TEntity> : System.Data.Objects.DataClasses.EntityReference where TEntity : class
[<System.Runtime.Serialization.DataContract>]
[<System.Serializable>]
type EntityReference<'Entity (requires 'Entity : null and 'Entity :> IEntityWithRelationships)> = class
    inherit EntityReference
[<System.Runtime.Serialization.DataContract>]
[<System.Serializable>]
type EntityReference<'Entity (requires 'Entity : null)> = class
    inherit EntityReference
Public NotInheritable Class EntityReference(Of TEntity)
Inherits EntityReference

類型參數

TEntity

參考的實體類型。The entity type of the reference.

繼承
EntityReference<TEntity>
屬性

範例

本範例將示範如何使用 EntityReference<TEntity> 物件來變更 SalesOrderHeader 物件與代表訂單送貨地址之相關 Address 物件間的關聯性。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

備註

當相關端的多重性為零或一時,導覽屬性就會傳回 EntityReference<TEntity> 物件。An EntityReference<TEntity> object is returned by a navigation property when the related end has a multiplicity of zero or one. 如需詳細資訊,請參閱 (CSDL) 的 NavigationProperty 元素 For more information, see NavigationProperty Element (CSDL).

EntityReference<TEntity> 物件可能會在關聯性 (Relationship) 的另一端具有對應的 EntityCollection<TEntity> (一對多關聯性 (One-To-Many Relationship)) 或 EntityReference<TEntity> (一對一關聯性 (One-To-One Relationship))。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. EntityReference<TEntity>EntityCollection<TEntity> 模型化相同關聯性的相對端時,關聯性的完整性就會在物件層級維護。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.

建構函式

EntityReference<TEntity>()

建立 EntityReference<TEntity> 的新執行個體。Creates a new instance of EntityReference<TEntity>.

屬性

EntityKey

傳回相關物件的索引鍵。Returns the key for the related object.

(繼承來源 EntityReference)
IsLoaded

取得值,這個值指出所有相關物件是否都已經載入。Gets a value that indicates whether all related objects have been loaded.

(繼承來源 RelatedEnd)
RelationshipName

取得這個相關端所參與之關聯性的名稱。Gets the name of the relationship in which this related end participates.

(繼承來源 RelatedEnd)
RelationshipSet

取得相關端中繼資料的參考。Gets a reference to the metadata for the related end.

(繼承來源 RelatedEnd)
SourceRoleName

取得位於關聯性來源端的角色名稱。Gets the role name at the source end of the relationship.

(繼承來源 RelatedEnd)
TargetRoleName

取得位於關聯性目標端的角色名稱。Gets the role name at the target end of the relationship.

(繼承來源 RelatedEnd)
Value

取得或設定這個 EntityReference<TEntity> 所傳回的相關物件。Gets or sets the related object returned by this EntityReference<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)

定義兩個所附加物件之間的關聯性。Defines a relationship between two attached objects.

(繼承來源 RelatedEnd)
CreateSourceQuery()

建立傳回相關物件的對等物件查詢。Creates an equivalent object query that returns the related object.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回 IEnumerator,它會逐一查看相關物件的集合。Returns an IEnumerator that iterates through the collection of related objects.

(繼承來源 RelatedEnd)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
Load()

在衍生類別中覆寫時,使用預設合併選項,將一個或多個相關物件載入相關端中。When overridden in a derived class, loads the related object or objects into the related end with the default merge option.

(繼承來源 RelatedEnd)
Load(MergeOption)

使用指定的合併選項,載入這個 EntityReference<TEntity> 的相關物件。Loads the related object for this EntityReference<TEntity> with the specified merge option.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean)

將相關實體合併至本機 EntityCollection<TEntity> 中。Merges related entities into the local EntityCollection<TEntity>.

(繼承來源 RelatedEnd)
OnDeserialized(StreamingContext)

在內部搭配 RelationshipManager 執行個體用來還原序列化實體物件。Used internally to deserialize entity objects along with the RelationshipManager instances.

(繼承來源 RelatedEnd)
OnRefDeserialized(StreamingContext)

這個方法是在內部用來序列化相關實體物件。This method is used internally to serialize related entity objects.

OnSerializing(StreamingContext)

這個方法是在內部用來序列化相關實體物件。This method is used internally to serialize related entity objects.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean)

判斷某個物件是否可以附加至本機集合或參考。Determines whether an object can be attached to the local collection or reference.

(繼承來源 RelatedEnd)
ValidateLoad<TEntity>(MergeOption, String)

確保一個或多個相關物件可以成功載入本機集合或參考中。Ensures that the related object or objects can be successfully loaded into the local collection or reference.

(繼承來源 RelatedEnd)
ValidateOwnerForAttach()

確保相關端所屬的物件支援附加作業。Ensures that the object to which the related end belongs supports an attach operation.

(繼承來源 RelatedEnd)

事件

AssociationChanged

對相關端進行變更時發生。Occurs when a change is made to a related end.

(繼承來源 RelatedEnd)

明確介面實作

IRelatedEnd.Add(IEntityWithRelationships)

將物件加入至相關端。Adds an object to the related end.

(繼承來源 RelatedEnd)
IRelatedEnd.Add(Object)

將物件加入至相關端。Adds an object to the related end.

(繼承來源 RelatedEnd)
IRelatedEnd.Attach(IEntityWithRelationships)

在相關端附加物件。Attaches an object at the related end.

(繼承來源 RelatedEnd)
IRelatedEnd.Attach(Object)

將物件附加至相關端。Attaches an object to the related end.

(繼承來源 RelatedEnd)
IRelatedEnd.CreateSourceQuery()

傳回 IEnumerable,它代表屬於相關端的物件。Returns an IEnumerable that represents the objects that belong to the related end.

(繼承來源 RelatedEnd)
IRelatedEnd.Remove(IEntityWithRelationships)

從位於相關端的集合中移除物件。Removes an object from the collection at the related end.

(繼承來源 RelatedEnd)
IRelatedEnd.Remove(Object)

如果物件是相關端的一部分,則從相關端移除物件。Removes an object from the related end if the object is part of the related end.

(繼承來源 RelatedEnd)

適用於