Monday, January 10, 2011

POCO

Those who have not referred the related post of this article can check it out : Entity Framework Posts

More about Entity programming

Those who have not referred the related post of this article can check it out : Entity Framework Posts
In this post we are going to check out the how Entity framework handles the Entities, how it detects the changed entities etc. Lets start with the ObjectContext.

ObjectContext


It is generated by Entity framework to provide facilities for querying and working with entity data as objects. By default the name of the context is the name which you have used when creating Entity Data Model for the first time in application.

By “querying entity data” is process of firing query against the data source and the query results in the data records returned by that query, then the result is materialized in the CLR types and then returned to the client. This process is also known as “materialization” and is being performed by EF.

image

We will take a look at more inside the above process in later blog, at this moment you you think ObjectContext as the gateway between your Entities and Database.

ObjectStateManager


For each ObjectContext there is an ObjectStateManager, it is used maintain the list of entities or you can say cache of entities, to track the changes made in the EntityObjects of that cache.

ObjectStateEntry


The ObjectStateEntry class is responsible for maintaining state and key information for entities. An
instance of the class is created for each entity type in the cache, and it has the sole responsibility of
tracking and maintaining the original values of the object, its relationships, and any properties that have been modified on the entity. The ObjectStateEntry class also tracks EntityState (such as whether the entity has been detached, deleted, modified, and so on) and EntityKey values.
One ObjectStateEntry can’t have the same key as another ObjectStateEntry within the same ObjectStateManager. When an entity is first created (enters the ObjectContext cache), the ObjectStateEntity takes a snapshot of it. This snapshot contains the original values of the entity, and as the entity is modified the current values are also stored. At the point of entity modification, the EntityState property is set to one of the following values:

  • Detached: The object exists, but it isn’t being tracked by the Object Services.

  • Unchanged: The object hasn’t been modified since it was loaded into the context or since the SaveChanges method was last called.

  • Added: The object is newly added to the context, and the SaveChanges method hasn’t been called.

  • Deleted: The object has been deleted from the context.

  • Modified: The object has changed, but the SaveChanges method hasn’t been called.


The great thing about working with entities is that the EF and the ObjectContext do all the change
tracking for you. As changes are made to the entity, the ObjectContext tracks these changes and
automatically sets the EntityState property. Before you work on some examples, the next section
examines change tracking and how changes are saved.

Lets check out how all these stuffs works behind the scene Ninja

[sourcecode language="csharp" firstline="1" padlinenumbers="true" collapse="true" gutter="true" htmlscript="false" light="true" toolbar="true" wraplines="true"] using(NorthwindEntities  db = new NorthwindEntities())
{
var product = db.Products.FirstOrDefault();
product.Name = "Coffee";
db.SaveChanges();
}[/sourcecode]

Over here we are creating ObjectContext of Northwind Entity Data Model. It is being done in line 1.

Once we have ObjectContext then in line 2 we are performing query against database to take first Product in the database. If the product returned by the query is not there in the Cache of ObjectStateManager, then it creates new Entity for that object, and one ObjectStateEntry for that newly created EntityObject. Because it is newly created object, the ObjectStateEntry’s EntityState equals to “Added

In line 3 we are modifying the name of the product to Coffee, if the product do not have Name = Coffee then this statement results in modified name of the EntityObject. So that means the  EntityState equals to “Modified” for the EntityObject.

In line 4 we have requested ObjectContext to save all the changes. This means ObjectContext have to take all the objects which have been modified or deleted and give the changes to the database. To do this ObjectContext traverse all the ObjectStateEntry objects in the cache to find the modified or deleted EntityObject with the help of EntityState property.

After that ObjectContext prepares the query which is compatible to the underlying data provider and then executes the query for us.