ObjectContext.CreateEntityKey Method (String, Object)


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

Creates the entity key for a specific object, or returns the entity key if it already exists.

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

member CreateEntityKey : 
        entitySetName:string *
        entity:Object -> EntityKey


Type: System.String

The fully qualified name of the entity set to which the entity object belongs.

Type: System.Object

The object for which the entity key is being retrieved.

Return Value

Type: System.Data.EntityKey

The EntityKey of the object.

Exception Condition

When either parameter is null.


When entitySetName is empty.


When the type of the entity object does not exist in the entity set.


When the entitySetName is not fully qualified.


When the entity key cannot be constructed successfully based on the supplied parameters.

If an EntityKey does not exist for the entity, the CreateEntityKey method creates a new key for it.

This method is used to determine whether an object that has the same EntityKey is already attached to the ObjectContext. If an object that has the same EntityKey is already attached, an exception is raised. Use the CreateEntityKey method to try to retrieve the EntityKey of the detached object before calling the Attach method.

The example in this topic is based on the AdventureWorks Sales Model. In this example, CreateEntityKey is used to retrieve the entity key of an existing object.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.5
Return to top