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>物件改變物件與代表訂單運送地址的相關Address物件之間的SalesOrderHeader關係。


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

備註

EntityReference<TEntity>當相關端點的重數為零或一時,該物件由導航性質回傳。 欲了解更多資訊,請參閱 NavigationProperty Element(CSDL)。

一個 EntityReference<TEntity> 物件可能在關係的另一端有對應 EntityCollection<TEntity> 的(一對多關係)或 EntityReference<TEntity> (一對一的關係)。 當 與 EntityReference<TEntity>EntityCollection<TEntity> 模型處於同一關係的對立端點時,該關係的完整性在物件層級得以維持。

此類別無法獲得繼承。

建構函式

名稱 Description
EntityReference<TEntity>()

建立一個新的 實例。EntityReference<TEntity>

屬性

名稱 Description
EntityKey

回傳相關物件的金鑰。

(繼承來源 EntityReference)
IsLoaded

會得到一個值,表示所有相關物件是否都已載入。

(繼承來源 RelatedEnd)
RelationshipName

取得該相關端參與的關係名稱。

(繼承來源 RelatedEnd)
RelationshipSet

會取得相關端的元資料參考。

(繼承來源 RelatedEnd)
SourceRoleName

在關係的原始端取得角色名稱。

(繼承來源 RelatedEnd)
TargetRoleName

在關係的目標端取得角色名稱。

(繼承來源 RelatedEnd)
Value

取得或設定由此 EntityReference<TEntity>回傳的相關物件。

方法

名稱 Description
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)

事件

名稱 Description
AssociationChanged

當變更發生在相關目的時。

(繼承來源 RelatedEnd)

明確介面實作

名稱 Description
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)

適用於