EntityReference<TEntity> 類別

定義

表示多重性為零或一之關聯的相關端。

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

參考的實體類型。

繼承
EntityReference<TEntity>
屬性

範例

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


// 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> 物件。 如需詳細資訊,請參閱 NavigationProperty 元素 (CSDL)

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

此類別無法獲得繼承。

建構函式

EntityReference<TEntity>()

建立 EntityReference<TEntity> 的新執行個體。

屬性

EntityKey

傳回相關物件的索引鍵。

(繼承來源 EntityReference)
IsLoaded

取得值,這個值指出所有相關物件是否都已經載入。

(繼承來源 RelatedEnd)
RelationshipName

取得這個相關端所參與之關聯性的名稱。

(繼承來源 RelatedEnd)
RelationshipSet

取得相關端中繼資料的參考。

(繼承來源 RelatedEnd)
SourceRoleName

取得位於關聯性來源端的角色名稱。

(繼承來源 RelatedEnd)
TargetRoleName

取得位於關聯性目標端的角色名稱。

(繼承來源 RelatedEnd)
Value

取得或設定這個 EntityReference<TEntity> 所傳回的相關物件。

方法

Attach(TEntity)

在物件內容中,建立兩個物件之間的多對一或一對一關聯性。

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

定義兩個所附加物件之間的關聯性。

(繼承來源 RelatedEnd)
CreateSourceQuery()

建立傳回相關物件的對等物件查詢。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回 IEnumerator,它會逐一查看相關物件的集合。

(繼承來源 RelatedEnd)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Load()

在衍生類別中覆寫時,使用預設合併選項,將一個或多個相關物件載入相關端中。

(繼承來源 RelatedEnd)
Load(MergeOption)

使用指定的合併選項,載入這個 EntityReference<TEntity> 的相關物件。

MemberwiseClone()

建立目前 Object 的淺層複製。

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

將相關實體合併至本機 EntityCollection<TEntity> 中。

(繼承來源 RelatedEnd)
OnDeserialized(StreamingContext)

在內部搭配 RelationshipManager 執行個體用來還原序列化實體物件。

(繼承來源 RelatedEnd)
OnRefDeserialized(StreamingContext)

這個方法是在內部用來序列化相關實體物件。

OnSerializing(StreamingContext)

這個方法是在內部用來序列化相關實體物件。

ToString()

傳回代表目前物件的字串。

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

判斷某個物件是否可以附加至本機集合或參考。

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

確保一個或多個相關物件可以成功載入本機集合或參考中。

(繼承來源 RelatedEnd)
ValidateOwnerForAttach()

確保相關端所屬的物件支援附加作業。

(繼承來源 RelatedEnd)

事件

AssociationChanged

對相關端進行變更時發生。

(繼承來源 RelatedEnd)

明確介面實作

IRelatedEnd.Add(IEntityWithRelationships)

將物件加入至相關端。

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

將物件加入至相關端。

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

在相關端附加物件。

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

將物件附加至相關端。

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

傳回 IEnumerable,它代表屬於相關端的物件。

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

從位於相關端的集合中移除物件。

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

如果物件是相關端的一部分,則從相關端移除物件。

(繼承來源 RelatedEnd)

適用於