How to: Create an EntityKey
The EntityKey class represents the key of an entity object. You can create an instance of EntityKey by using the class constructors, or you can use the static CreateEntityKey method of ObjectContext to generate an EntityKey for a specific object. The entity key is used to attach an object or to return a specific object from the data source. For more information, see Working with Entity Keys.
The examples in this topic are based on the Adventure Works Sales Model. To run the code in this example, 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.
The following example creates an instance of EntityKey using a specified key/value pair and the qualified entity set name. This key is then used to retrieve the object itself.
For an independent association, use the method described in the following example to define a relationship. For a foreign key association, define relationships by setting the value of the foreign key properties on the dependent object. For more information, see Defining and Managing Relationships.
The following example creates an instance of EntityKey using a specified key name, key value, and the qualified entity set name. This key is then used to attach an object and define a relationship.
The following example creates an instance of EntityKey using key values from a detached object. This key is then used to retrieve an attached instance of the object.