ObjectContext.GetObjectByKey Method (EntityKey)


Returns an object that has the specified entity key.

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

member GetObjectByKey : 
        key:EntityKey -> Object


Type: System.Data.EntityKey

The key of the object to be found.

Return Value

Type: System.Object

An Object that is an instance of an entity type.

Exception Condition

The key parameter is null.


The object is not found in either the ObjectStateManager or the data source.

GetObjectByKey tries to retrieve an object that has the specified EntityKey from the ObjectStateManager. If the object is currently not loaded into the object context, a query is executed in an attempt to return the object from the data source. For more information, see Object Queries.

GetObjectByKey raises an ObjectNotFoundException when the object cannot be found. To avoid handling this exception, instead use the TryGetObjectByKey method.

This method will return objects in the Deleted state.

A temporary key cannot be used to return an object from the data source.

This example is based on the AdventureWorks Sales Model. The example creates an EntityKey for an entity of the given type and then fetches an entity by key.

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

.NET Framework
Available since 3.5
Return to top