EntityKey 類別

定義

提供實體 (Entity) 類型執行個體 (Instance) 之物件的持久參考。

public ref class EntityKey sealed : IEquatable<System::Data::EntityKey ^>
[System.Runtime.Serialization.DataContract(IsReference=true)]
[System.Serializable]
public sealed class EntityKey : IEquatable<System.Data.EntityKey>
[<System.Runtime.Serialization.DataContract(IsReference=true)>]
[<System.Serializable>]
type EntityKey = class
    interface IEquatable<EntityKey>
Public NotInheritable Class EntityKey
Implements IEquatable(Of EntityKey)
繼承
EntityKey
屬性
實作

範例

這些範例示範如何建立和使用 EntityKey

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    Object entity = null;
    IEnumerable<KeyValuePair<string, object>> entityKeyValues =
        new KeyValuePair<string, object>[] {
            new KeyValuePair<string, object>("SalesOrderID", 43680) };

    // Create the  key for a specific SalesOrderHeader object.
    EntityKey key = new EntityKey("AdventureWorksEntities.SalesOrderHeaders", entityKeyValues);

    // Get the object from the context or the persisted store by its key.
    if (context.TryGetObjectByKey(key, out entity))
    {
        Console.WriteLine("The requested " + entity.GetType().FullName +
            " object was found");
    }
    else
    {
        Console.WriteLine("An object with this key " +
            "could not be found.");
    }
}
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Create the key that represents the order.
        EntityKey orderKey =
            new EntityKey("AdventureWorksEntities.SalesOrderHeaders",
                "SalesOrderID", orderId);

        // Create the stand-in SalesOrderHeader object
        // based on the specified SalesOrderID.
        SalesOrderHeader order = new SalesOrderHeader();
        order.EntityKey = orderKey;

        // Assign the ID to the SalesOrderID property to matche the key.
        order.SalesOrderID = (int)orderKey.EntityKeyValues[0].Value;

        // Attach the stand-in SalesOrderHeader object.
        context.SalesOrderHeaders.Attach(order);

        // Create a new SalesOrderDetail object.
        // You can use the static CreateObjectName method (the Entity Framework
        // adds this method to the generated entity types) instead of the new operator:
        // SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
        //                                         Guid.NewGuid(), DateTime.Today));
        SalesOrderDetail detail = new SalesOrderDetail
        {
            SalesOrderID = orderId,
            SalesOrderDetailID = 0,
            OrderQty = 2,
            ProductID = 750,
            SpecialOfferID = 1,
            UnitPrice = (decimal)2171.2942,
            UnitPriceDiscount = 0,
            LineTotal = 0,
            rowguid = Guid.NewGuid(),
            ModifiedDate = DateTime.Now
        };

        order.SalesOrderDetails.Add(detail);

        context.SaveChanges();
    }
    catch (InvalidOperationException)
    {
        Console.WriteLine("Ensure that the key value matches the value of the object's ID property.");
    }
    catch (UpdateException)
    {
        Console.WriteLine("An error has occurred. Ensure that an object with the '{0}' key value exists.",
        orderId);
    }
}

備註

EntityKey 物件是不可變動的。也就是說,建構這些物件之後,就無法修改它們。

如需詳細資訊,請參閱 使用實體密鑰

建構函式

EntityKey()

初始化 EntityKey 類別的新執行個體。

EntityKey(String, IEnumerable<EntityKeyMember>)

使用實體集名稱和 EntityKey 物件的 IEnumerable<T> 集合,初始化 EntityKeyMember 類別的新執行個體。

EntityKey(String, IEnumerable<KeyValuePair<String,Object>>)

使用實體集名稱和泛型 EntityKey 集合,初始化 KeyValuePair 類別的新執行個體。

EntityKey(String, String, Object)

使用實體集名稱和特定實體索引鍵組,初始化 EntityKey 類別的新執行個體。

欄位

EntityNotValidKey

簡單的 EntityKey,可識別失敗 TREAT 作業所產生的實體。

NoEntitySetKey

用以識別唯讀實體的單一 EntityKey

屬性

EntityContainerName

取得或設定實體容器的名稱。

EntityKeyValues

取得或設定與這個 EntityKey 關聯的索引鍵值。

EntitySetName

取得或設定實體集的名稱。

IsTemporary

取得值,這個值表示 EntityKey 是否為暫存。

方法

Equals(EntityKey)

傳回值,這個值指出此執行個體是否與指定的 EntityKey 相等。

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

GetEntitySet(MetadataWorkspace)

從給定的中繼資料 (Metadata) 工作空間中取得此實體索引鍵的實體集。

GetHashCode()

做為目前 EntityKey 物件的雜湊函式。 GetHashCode() 適合用於雜湊演算法和諸如雜湊資料表這類的資料結構。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnDeserialized(StreamingContext)

可用來還原序列化 EntityKey 的 Helper 方法。

OnDeserializing(StreamingContext)

可用來還原序列化 EntityKey 的 Helper 方法。

ToString()

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

(繼承來源 Object)

運算子

Equality(EntityKey, EntityKey)

比較兩個 EntityKey 物件。

Inequality(EntityKey, EntityKey)

比較兩個 EntityKey 物件。

適用於