Creating Entity Key in Entity Framework

Introduction

Entity Key is a property (or set of properties) of the entity type to identify an object in the object context. The Entity Key must be a unique identification of the entity type instance within the entity set at runtime.

The following points must considered while creating an Entity Key:

  • An Entity Key must contain a set of non nullable, primitive types and immutable properties.
  • A duplicate Entity Key within an entity set is not allowed.
  • The properties used to create an Entity Key for the entity type never change and also surrogate keys are not supported.

Create an Entity Key for the entity set

The Entity Key represents the key of the entity object. We can create an entity using one of two methods.

1. Using CreateEntityKey method of Object Context

The "CreateEntityKey" method creates the Entity Key for the given object. If the Entity Key does not exist for the entity then this method creates a new key for that entity.
The "CreateEntityKey" method has the following two parameters:

  • EntitySetName : The name of the entity set
  • Entity: The object for which the key is being generated

Example

Suppose I have two tables, MasterTable and DetailTable. The relation between the tables and primary key detail is shown in the following figure.


  1. using (Entities context = new Entities())  
  2. {  
  3.     MasterTable m = new MasterTable();  
  4.     m.MasterId = 1;  
  5.     EntityKey key = context.CreateEntityKey("MasterTables", m);  
  6.     object data;  
  7.     context.TryGetObjectByKey(key, out data);  
  8. } 

2. By Creating an instance of Entity Key

If we want to create an Entity Key using an instance method then we need to assign the three properties, EntityContainerName, EntitySetName and EntityKeyValues to the Entity Key object. EntityContainerName is the name of the entity container for the entity to which the Entity Key belongs. EntitySetName is the name of the entity set to which the Entity Key belongs. EntityKeyValues is an array of key values associated with Entity Key.

Example

  1. using (Entities context = new Entities())  
  2. {  
  3.      EntityKey entityKey = new EntityKey();  
  4.      entityKey.EntityContainerName = "Entities";  
  5.      entityKey.EntitySetName = "MasterTables";  
  6.      entityKey.EntityKeyValues = new EntityKeyMember[] { new EntityKeyMember("MasterId", 1) };  
  7.     object data;  
  8.     context.TryGetObjectByKey(entityKey, out data);  
  9. } 
instance of Entity key 

Output

Output

Retrieve an Entity Key for the entity object that has a composite key.

In the preceding example, the DetailTable entity has a composite primary key on DetailId and MasterId.
  1. // Using CreateEntityKey method of Object Context  
  2. using (Entities context = new Entities())  
  3. {  
  4.      DetailTable d = new DetailTable();  
  5.      d.DetailId = 1;  
  6.      d.MasterId = 1;  
  7.      EntityKey key = context.CreateEntityKey("DetailTables", d);  
  8.     object data;  
  9.     context.TryGetObjectByKey(key, out data);  
  10. }  
  11. // By Creating an instance of Entity key  
  12. using (Entities context = new Entities())  
  13. {  
  14.     EntityKey entityKey = new EntityKey();  
  15.     entityKey.EntityContainerName = "Entities";  
  16.     entityKey.EntitySetName = "DetailTables";  
  17.     entityKey.EntityKeyValues = new EntityKeyMember[]  
  18. {  
  19.     new EntityKeyMember("DetailId", 1),  
  20.     new EntityKeyMember("MasterId", 1)  
  21. };  
  22. object data;  
  23. context.TryGetObjectByKey(entityKey, out data);  
  24. } 

Conclusion

Using the preceding two described methods we can generate an Entity Key of any entity object and using the Entity Key we can retrieve the entire entity from the object context. 


Similar Articles