Export (0) Print
Expand All

How to: Return a Specific Object Using its Key

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic shows how to retrieve an entity by its key values instead of explicitly creating and executing an object query. The examples in this topic are based on the AdventureWorks Sales Model. To run the code in these examples, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define the Model and Mapping Files. You can also use the Entity Data Model Wizard to define the AdventureWorks Sales Model. For more information, see How to: Use the Entity Data Model Wizard.

Example

This example uses the GetObjectByKey method on ObjectContext to return an object with the specified EntityKey into the object context. This example handles the ObjectNotFoundException that occurs when the supplied EntityKey does not correspond to an existing entity. To avoid the ObjectNotFoundException, call TryGetObjectByKey, which returns false instead of raising an exception.

See Also

Show:
© 2014 Microsoft