Saturday 12 July 2014

Life Cycle of Entity

During entity’s lifetime, each entity has an entity state based on operation performed on it via Context (ObjectContext). The entity state is an enum of type System.Data.EntityState that declares the following values:


  1. Added
  2. Deleted
  3. Modified
  4. Unchanged
  5. Detached


The Context not only holds the reference to all the objects retrieved from the database but also it holds the entity states and maintains modifications to the properties of the entity. This feature is known as Change Tracking.
The change in entity state from the Unchanged to the Modified state is the only state that’s automatically handled by the context. All other changes must be made explicitly using proper methods of ObjectContext:


Sr.
ObjectContext Methods
Description
EntityState
1
AddObject
Adds an entity to the context
Added
2
Attach
Attaches an entity to the context
Unchanged
3
ApplyCurrentValues
Replace currently attached entity’s scalar value with the property values of detached entity.
Modified
4
ApplyOriginalValues
It applies original database values to attached entity’s properties.
Unchanged
5
DeleteObject
Delete the object from context.
Deleted
6
AcceptAllChanges
pushes the current values of every attached entity into the original values.
Unchanged
7
ChangeState or ChangeObjectState
Change an entity from one state to another without any restrictions (except for Detached)
Based on passed state
8
Detach
Removes an entity from the context.
Detached

No comments:

Post a Comment