EntityKey EntityKey EntityKey EntityKey Class

定義

提供實體 (Entity) 類型執行個體 (Instance) 之物件的持久參考。Provides a durable reference to an object that is an instance of an entity type.

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>
type EntityKey = class
    interface IEquatable<EntityKey>
Public NotInheritable Class EntityKey
Implements IEquatable(Of EntityKey)
繼承
EntityKeyEntityKeyEntityKeyEntityKey
屬性
實作

範例

這些範例根據AdventureWorks Sales ModelThese examples are based on the AdventureWorks Sales Model. 範例會示範如何建立和使用EntityKeyThe examples show you how to create and use an 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 context As New AdventureWorksEntities()
    Dim entity As Object = Nothing
    Dim entityKeyValues As IEnumerable(Of KeyValuePair(Of String, Object)) = _
        New KeyValuePair(Of String, Object)() {New KeyValuePair(Of String, Object)("SalesOrderID", 43680)}

    ' Create the key for a specific SalesOrderHeader object. 
    Dim key As New EntityKey("AdventureWorksEntities.SalesOrderHeaders", entityKeyValues)

    ' Get the object from the context or the persisted store by its key. 
    If context.TryGetObjectByKey(key, entity) Then
        Console.WriteLine("The requested " & entity.GetType().FullName & " object was found")
    Else
        Console.WriteLine("An object with this key could not be found.")
    End If
End Using
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);
    }
}
Using context As New AdventureWorksEntities()
    Try
        ' Create the key that represents the order. 
        Dim orderKey As New EntityKey("AdventureWorksEntities.SalesOrderHeaders", "SalesOrderID", orderId)

        ' Create the stand-in SalesOrderHeader object 
        ' based on the specified SalesOrderID. 
        Dim order As New SalesOrderHeader()
        order.EntityKey = orderKey

        ' Assign the ID to the SalesOrderID property to matche the key. 
        order.SalesOrderID = CInt(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)); 
        Dim detail = New SalesOrderDetail With
        {
            .SalesOrderID = 0,
            .SalesOrderDetailID = 0,
            .OrderQty = 2,
            .ProductID = 750,
            .SpecialOfferID = 1,
            .UnitPrice = CDec(2171.2942),
            .UnitPriceDiscount = 0,
            .LineTotal = 0,
            .rowguid = Guid.NewGuid(),
            .ModifiedDate = DateTime.Now
        }

        order.SalesOrderDetails.Add(detail)

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

備註

EntityKey 物件是不可變動的。也就是說,建構這些物件之後,就無法修改它們。The EntityKey objects are immutable; that is, after they are constructed they cannot be modified.

如需詳細資訊,請參閱 < 使用實體索引鍵For more information, see Working with Entity Keys.

建構函式

EntityKey() EntityKey() EntityKey() EntityKey()

初始化 EntityKey 類別的新執行個體。Initializes a new instance of the EntityKey class.

EntityKey(String, IEnumerable<EntityKeyMember>) EntityKey(String, IEnumerable<EntityKeyMember>) EntityKey(String, IEnumerable<EntityKeyMember>) EntityKey(String, IEnumerable<EntityKeyMember>)

使用實體集名稱和 EntityKey 物件的 IEnumerable<T> 集合,初始化 EntityKeyMember 類別的新執行個體。Initializes a new instance of the EntityKey class with an entity set name and an IEnumerable<T> collection of EntityKeyMember objects.

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

使用實體集名稱和泛型 EntityKey 集合,初始化 KeyValuePair 類別的新執行個體。Initializes a new instance of the EntityKey class with an entity set name and a generic KeyValuePair collection.

EntityKey(String, String, Object) EntityKey(String, String, Object) EntityKey(String, String, Object) EntityKey(String, String, Object)

使用實體集名稱和特定實體索引鍵組,初始化 EntityKey 類別的新執行個體。Initializes a new instance of the EntityKey class with an entity set name and specific entity key pair.

欄位

EntityNotValidKey EntityNotValidKey EntityNotValidKey EntityNotValidKey

簡單的 EntityKey,可識別失敗 TREAT 運算所產生的實體。A simple EntityKey identifying an entity that resulted from a failed TREAT operation.

NoEntitySetKey NoEntitySetKey NoEntitySetKey NoEntitySetKey

用以識別唯讀實體的單一 EntityKeyA singleton EntityKey by which a read-only entity is identified.

屬性

EntityContainerName EntityContainerName EntityContainerName EntityContainerName

取得或設定實體容器的名稱。Gets or sets the name of the entity container.

EntityKeyValues EntityKeyValues EntityKeyValues EntityKeyValues

取得或設定與這個 EntityKey 關聯的索引鍵值。Gets or sets the key values associated with this EntityKey.

EntitySetName EntitySetName EntitySetName EntitySetName

取得或設定實體集的名稱。Gets or sets the name of the entity set.

IsTemporary IsTemporary IsTemporary IsTemporary

取得值,這個值表示 EntityKey 是否為暫存。Gets a value that indicates whether the EntityKey is temporary.

方法

Equals(EntityKey) Equals(EntityKey) Equals(EntityKey) Equals(EntityKey)

傳回值,這個值指出此執行個體是否與指定的 EntityKey 相等。Returns a value that indicates whether this instance is equal to a specified EntityKey.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

GetEntitySet(MetadataWorkspace) GetEntitySet(MetadataWorkspace) GetEntitySet(MetadataWorkspace) GetEntitySet(MetadataWorkspace)

從給定的中繼資料 (Metadata) 工作空間中取得此實體索引鍵的實體集。Gets the entity set for this entity key from the given metadata workspace.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為目前 EntityKey 物件的雜湊函式。Serves as a hash function for the current EntityKey object. GetHashCode() 適合用於雜湊演算法和諸如雜湊資料表這類的資料結構。GetHashCode() is suitable for hashing algorithms and data structures such as a hash table.

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnDeserialized(StreamingContext) OnDeserialized(StreamingContext) OnDeserialized(StreamingContext) OnDeserialized(StreamingContext)

可用來還原序列化 EntityKey 的 Helper 方法。Helper method that is used to deserialize an EntityKey.

OnDeserializing(StreamingContext) OnDeserializing(StreamingContext) OnDeserializing(StreamingContext) OnDeserializing(StreamingContext)

可用來還原序列化 EntityKey 的 Helper 方法。Helper method that is used to deserialize an EntityKey.

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

運算子

Equality(EntityKey, EntityKey) Equality(EntityKey, EntityKey) Equality(EntityKey, EntityKey) Equality(EntityKey, EntityKey)

比較兩個 EntityKey 物件。Compares two EntityKey objects.

Inequality(EntityKey, EntityKey) Inequality(EntityKey, EntityKey) Inequality(EntityKey, EntityKey) Inequality(EntityKey, EntityKey)

比較兩個 EntityKey 物件。Compares two EntityKey objects.

適用於