Explain lifecycle of entity framework?

Please give a brief description of the Entity Framework lifecycle with a suitable image.

Last updated:2/5/2020 12:47:49 AM

1 Answers

Nishi Tiwari
Nishi Tiwari

The Entity Lifecycle is defined as the process in which an Entity is created, added, modified, deleted, etc. These Entities have many states during its lifetime. So, before looking at how to retrieve entity state, look at what is entity state. These states are an enum of type System.Data.EntityState that declares the following values −

• Added: An entity is marked as added.

• Deleted: An entity is marked as deleted.

• Modified: The entity has been modified.

• Unchanged: The entity hasn’t been modified.

• Detached: The entity isn’t tracked.

State Changes in the Entity Lifecycle

When sometimes the state of entities are set automatically by the context, but it can also be modified manually by the developer. If all the combinations of switches from one state to another are possible, but some of them are meaningless for example, the Added entity to the Deleted state, or vice versa.

Unchanged State

  •  Whenever an entity is Unchanged, it is bound to the context but it hasn’t been modified.
  • An entity retrieved from the database is in this state is by default.
  •   If an entity is attached to the context (with the Attach method), it similarly is in the Unchanged state.
  •  The context can’t track changes to objects which it doesn’t reference, so when they are attached it assumes they’re Unchanged.

Detached State

  •  It is the default state of a newly created entity because the context can’t track the creation of any object in your code.
  •  It is true even if we instantiate the entity inside a using block of the context.
  •  It is the state of entities retrieved from the database when tracking is disabled.
  •  Whenever an entity is detached, it isn’t bound to the context, so its state isn’t tracked.
  •  Detached can be disposed of, modified, used in combination with other classes, or used in any other way you might need.
  •  There is no context tracking it, so it has no meaning to Entity Framework.

Added State

  •  Whenever an entity is in the Added state, we have few options. In fact, we can only detach it from the context.
  •  Even if we modify some property, the state remains Added, because moving it to Modified, Unchanged, or deleted makes no sense.
  •  It is a new entity and has no correspondence with a row in the database.
  •  It is a fundamental prerequisite for being in one of those.

Modified State

  •  Whenever an entity is modified, it means it was in Unchanged state and then some property was changed.
  •  After an entity enters the Modified state, it can move to the Detached or Deleted state, but it can’t roll back to the Unchanged state even if we manually restore the original values.
  •  Modified state can’t even be changed to Added unless we detach and add the entity to the context, because a row with this ID already exists in the database, and we would get a runtime exception when persisting it.

Deleted State

  •  The entity enters the Deleted state because it was Unchanged or Modified and then the DeleteObject method was used.
  •  It is the most restrictive state, because it’s pointless changing from this state to any other value but Detached.

Explain lifecycle of entity framework?