DEV Community

Sanjeevi Subramani
Sanjeevi Subramani

Posted on • Originally published at lkgforit.com on

Entity Framework Interview Notes

Entity Framework is an Object Relational Mapper (ORM) which is a type of tool that simplifies mapping between objects in your software to the tables and columns of a relational database.

a. Types:

POCO just auto create in visual studio A POCO entity is a class that doesnt depend on any framework-specific base class. It is like any other normal .NET CLR class, which is why it is called Plain Old CLR Objects.

Dynamic change some properties attribute based

b. Entity Lifecycle

States in lifetime

Added: The entity is marked as added.

Deleted: The entity is marked as deleted.

Modified: The entity has been modified.

Unchanged: The entity hasnt been modified.

Detached: The entity isnt tracked.

c. State:

Unchanged State

o When an entity is Unchanged, its bound to the context but it hasnt been modified.

o By default, an entity retrieved from the database is in this state.

o When an entity is attached to the context (with the Attach method), it similarly is in the Unchanged state.

o The context cant track changes to objects that it doesnt reference, so when theyre attached it assumes theyre Unchanged.

Detached State

o Detached is the default state of a newly created entity because the context cant track the creation of any object in your code.

o This is true even if you instantiate the entity inside a using block of the context.

o Detached is even the state of entities retrieved from the database when tracking is disabled.

o When an entity is detached, it isnt bound to the context, so its state isnt tracked.

o It can be disposed of, modified, used in combination with other classes, or used in any other way you might need.

o Because there is no context tracking it, it has no meaning to Entity Framework.

Added State

o When an entity is in the Added state, you have few options. In fact, you can only detach it from the context.

o Naturally, even if you modify some property, the state remains Added, because moving it to Modified, Unchanged, or Deleted makes no sense.

o Its a new entity and has no correspondence with a row in the database.

o This is a fundamental prerequisite for being in one of those states (but this rule isnt enforced by the context).

Modified State

o When an entity is modified, that means it was in Unchanged state and then some property was changed.

o After an entity enters the Modified state, it can move to the Detached or Deleted state, but it cant roll back to the Unchanged state even if you manually restore the original values.

o It cant even be changed to Added, unless you detach and add the entity to the context, because a row with this ID already exists in the database, and you would get a runtime exception when persisting it.

Deleted State

o An entity enters the Deleted state because it was Unchanged or Modified and then the DeleteObject method was used.

o This is the most restrictive state, because its pointless changing from this state to any other value but Detached.

d. Entity Framework Approach types:

Code First

Database First

Model First

Discussion (0)