Entity Lifecycle in EntityFrame Work:--
Before working on entity framework on asp.net, It's important to know the entity (like any table, list etc.) life cycle and how it is managed by the entity framework. During entitys lifetime, each entity has an entity state based on the operation performed on it via Context(dbcontext, objectContext). The entity state is an enum of type System. Data. Entity State that declares the following values:
- Added
- Deleted
- Modified
- Unchanged
- Detached
In Entity framework the context holds the reference of the all objects those are retrieved from database. The context also responsible for entity states and maintains modifications to the properties of the entity. This is called Change Tracking.
In entity framework the Unchanged to the modification state automatically handled by the context.Below figure illustrate how the operation performed in entity and which state is turns.
- AddObject - AddObject is used to Adds an entity to the context. It's entity state is 'Added'.
- Attach - The entity state is unchanged when Attaches an entity to the context.
- ApplyCurrentValues - Replace currently attached entitys scalar value with the current property values of detached entity. It's entity state is 'Modified'.
- ApplyOriginalValues - It applies original database values to attached entitys properties. It's entity state is 'Unchanged'.
- DeleteObject - Delete the object from context. It's entity state is 'Deleted'.
- AcceptAllChanges - Update the current values of every attached entity into the original values. It's entity state is 'Unchanged'.
- ChangeState or ChangeObjectState - Change an entity from one state to another without any restrictions (except for Detached). It's entity state is 'Based on passed state'.
- Detach - Removes an entity from the context. It's entity state is 'Detached'.
0 Comment(s)