Entity Lifecycle

Before entity life cycle, we should also know about the context life cycle.

Context Lifetime

Context lifetime begins when its instance is created and ends when the instance is either disposed or garbage-collected. Context lifetime is a very crucial decision to make when we use ORMs. The context performs like an entity cache, so it holds references to all the loaded entities which may grow very fast in memory consumption and it can also cause memory leaks.

Entity Lifecycle

The Entity Lifecycle describes the process in which an Entity is created, added, modified, deleted, etc. Entities have different states during its lifetime. The entity state is an enum of type System.Data.Entity.EntityState that includes the following values:

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

 The Context holds the entity states and maintains modifications made 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 is automatically handled by the context. All other changes must be made explicitly using proper methods of DbContext and DbSet.

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More