EntityKey Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Provides a durable reference to an object that is an instance of an entity type.

Namespace:   System.Data
Assembly:  System.Data.Entity (in System.Data.Entity.dll)


<DataContractAttribute(IsReference := True)>
Public NotInheritable Class EntityKey
	Implements IEquatable(Of EntityKey)


Initializes a new instance of the EntityKey class.

System_CAPS_pubmethodEntityKey(String, IEnumerable(Of EntityKeyMember))

Initializes a new instance of the EntityKey class with an entity set name and an IEnumerable(Of T) collection of EntityKeyMember objects.

System_CAPS_pubmethodEntityKey(String, IEnumerable(Of KeyValuePair(Of String, Object)))

Initializes a new instance of the EntityKey class with an entity set name and a generic T:System.Collections.Generic.KeyValuePair collection.

System_CAPS_pubmethodEntityKey(String, String, Object)

Initializes a new instance of the EntityKey class with an entity set name and specific entity key pair.


Gets or sets the name of the entity container.


Gets or sets the key values associated with this EntityKey.


Gets or sets the name of the entity set.


Gets a value that indicates whether the EntityKey is temporary.


Returns a value that indicates whether this instance is equal to a specified EntityKey.


Returns a value that indicates whether this instance is equal to a specified object. (Overrides Object.Equals(Object).)


Gets the entity set for this entity key from the given metadata workspace.


Serves as a hash function for the current EntityKey object. GetHashCode is suitable for hashing algorithms and data structures such as a hash table.(Overrides Object.GetHashCode().)


Gets the Type of the current instance.(Inherited from Object.)


Helper method that is used to deserialize an EntityKey.


Helper method that is used to deserialize an EntityKey.


Returns a string that represents the current object.(Inherited from Object.)


A simple EntityKey identifying an entity that resulted from a failed TREAT operation.


A singleton EntityKey by which a read-only entity is identified.

System_CAPS_puboperatorSystem_CAPS_staticEquality(EntityKey, EntityKey)

Compares two EntityKey objects.

System_CAPS_puboperatorSystem_CAPS_staticInequality(EntityKey, EntityKey)

Compares two EntityKey objects.

The EntityKey objects are immutable; that is, after they are constructed they cannot be modified.

For more information, see Working with Entity Keys.

These examples are based on the AdventureWorks Sales Model. The examples show you how to create and use an EntityKey.

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")
        Console.WriteLine("An object with this key could not be found.")
    End If
End Using
Using context As New AdventureWorksEntities()
        ' 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. 

        ' 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


    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 occured. Ensure that an object with the '{0}' key value exists.", orderId)
    End Try
End Using

.NET Framework
Available since 3.5

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top