Entity Framework EntityKey object
What is an EntityKey ?
In a relational database we use primary keys to uniquely identify each record in the database table. Similarly, the primary key of an entity is called EntityKey and it uniquely identifies an entity. Every entity in an EDM contains an EntityKey which comprises of one or more scalar properties. These EntityKeys are used in applications to keep track of entities. A sample Entity Data Model with the name SOMModel.edmx is displayed in the designer window is shown below.
If you observe the Conceptual Schema of the EDM in the designer view of the .edmx file, you will see the EntityContainer, EntitySets and Entities are defined as meta data. Select the .edmx file and open it using XML editor. The designer view of the .edmx file is shown in the picture below.
In the CSDL file which is listed below, the EntityType element describes the structure of an entity. An Entity has one or more properties and these are defined by Property element. Similar to an unique key which is used to identify a row in a table, EntityType also defines a key using one or more properties using Key element. If the key for the entity is defined using more than one property, it is called composite entity key. This key uniquely identifies an entity within the entity collection (EntitySet) and is represented as EntityKey. So, Customer entity type has a key that is based on CustomerID property of the entity.
If you open the property window for the CustomerID property as shown below, the value of Entity Key property will be True. This property uniquely identifies the customer instance in the EntitySet.
How to get entity key and its value using EntityObject.EntityKey Property
At the time of materializing the entity objects, the entity key is materialized into an instance of the EntityKey class by the Entity Framework. We can access this EntityKey from the EntityKey property of the entity object. Using EntityKey Class, we can access the following information.
Using EntityKeyValues property, we can retrieve key names and values of EntityKey of an entity. Note that EntityKeyValues property is an IEnumerable collection. We first get EntityKey Name and Value for an Entity using the following statement.
In a relational database we use primary keys to uniquely identify each record in the database table. Similarly, the primary key of an entity is called EntityKey and it uniquely identifies an entity. Every entity in an EDM contains an EntityKey which comprises of one or more scalar properties. These EntityKeys are used in applications to keep track of entities. A sample Entity Data Model with the name SOMModel.edmx is displayed in the designer window is shown below.
If you observe the Conceptual Schema of the EDM in the designer view of the .edmx file, you will see the EntityContainer, EntitySets and Entities are defined as meta data. Select the .edmx file and open it using XML editor. The designer view of the .edmx file is shown in the picture below.
In the CSDL file which is listed below, the EntityType element describes the structure of an entity. An Entity has one or more properties and these are defined by Property element. Similar to an unique key which is used to identify a row in a table, EntityType also defines a key using one or more properties using Key element. If the key for the entity is defined using more than one property, it is called composite entity key. This key uniquely identifies an entity within the entity collection (EntitySet) and is represented as EntityKey. So, Customer entity type has a key that is based on CustomerID property of the entity.
If you open the property window for the CustomerID property as shown below, the value of Entity Key property will be True. This property uniquely identifies the customer instance in the EntitySet.
How to get entity key and its value using EntityObject.EntityKey Property
At the time of materializing the entity objects, the entity key is materialized into an instance of the EntityKey class by the Entity Framework. We can access this EntityKey from the EntityKey property of the entity object. Using EntityKey Class, we can access the following information.
- EntityContainerName
- EntityKeyValues
- EntitySetName
Using EntityKeyValues property, we can retrieve key names and values of EntityKey of an entity. Note that EntityKeyValues property is an IEnumerable collection. We first get EntityKey Name and Value for an Entity using the following statement.
More Resources
- POCO class in Entity Framework
- POCO vs Entity Objects with comparison table
- Rules to be followed while creating POCOs - 4 important rules
- How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
- How to load related POCO entities - loading patterns and their differences in their usage with examples
- How to perform Lazy Loading with POCO classes
- What is Change tracking in POCOs, ways of tracking
- Snapshot change tracking with DetectChanges() method
- Change tracking with proxies, How to create proxies?
- Instantiate POCO classes using ObjectContext.CreateObject method
- Using the DetectChanges() method to Fix-up Relationship in poco entities
- Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
- How to create an EntityKey in Entity Framework
- Loading Related entities in EF
- How to change the state of an entity using ChangeObjectState method of ObjectStateManager
- ChangeState method of ObjectStateEntry class
- Update an entity in a disconnected scenario using ApplyCurrentValues method
- Using ApplyOriginalValues method
- ObjectStateEntry class
- Entity state in Entity Framework - what, how, why?
- AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net