How to create change tracking proxies
We have seen why we should track changes in POCO entities and ways of change tracking mechanisms in a related article. There are two ways to track changes. One is Snapshot Change Tracking using DetectChanges method and without using proxies and the other is Change Tracking with proxies. This article demonstrates another way of change-tracking mechanism by proxies.
As we know, whenever a scalar property is changed, the entity inherited from EntityObject notifies the ObjectContext about the change. This will enable ObjectContext to keep track of entity state. In the same way, to enable entities to notify their changes, we create change tracking proxies. To create these proxies, we should follow a few rules.
The SOM model used in this article contains Customer and SalesOrder entities having one-to-many relationship between them.
Every property should be declared with virtual keyword and we know it will not be a pure POCO class. When we declare the property as virtual, EF creates a dynamic proxy class at runtime. This proxy class gives not only the change notification functionality but also relationship fix up and Lazy loading capabilities to the class. Because of these created proxy classes, entities notify their changes automatically and continuously (the same way EntityObject class does).
Next, I will demonstrate How Change tracking proxies work, with an example. Firstly, we create classes as given in the code above.
Explanation: 1.The code retrieves the customer information whose CustomerID is 16 and changes the name of the customer.
2. At this point of time, if we display the ObjectStateEntry objects, you will see that they are current (the values in the entities and ObjectStateEntry objects are same). The synchronization happens automatically as proxies notify ObjectStateManager about changes in entities and we need not call DetectChanges() method to synchronize them. The comparision between original and current values will not happen and this is a more efficient mechanism. However, proxies create serialization problems, so they cannot be used in enterprise applications.
As we know, whenever a scalar property is changed, the entity inherited from EntityObject notifies the ObjectContext about the change. This will enable ObjectContext to keep track of entity state. In the same way, to enable entities to notify their changes, we create change tracking proxies. To create these proxies, we should follow a few rules.
The SOM model used in this article contains Customer and SalesOrder entities having one-to-many relationship between them.
Every property should be declared with virtual keyword and we know it will not be a pure POCO class. When we declare the property as virtual, EF creates a dynamic proxy class at runtime. This proxy class gives not only the change notification functionality but also relationship fix up and Lazy loading capabilities to the class. Because of these created proxy classes, entities notify their changes automatically and continuously (the same way EntityObject class does).
Next, I will demonstrate How Change tracking proxies work, with an example. Firstly, we create classes as given in the code above.
Explanation: 1.The code retrieves the customer information whose CustomerID is 16 and changes the name of the customer.
2. At this point of time, if we display the ObjectStateEntry objects, you will see that they are current (the values in the entities and ObjectStateEntry objects are same). The synchronization happens automatically as proxies notify ObjectStateManager about changes in entities and we need not call DetectChanges() method to synchronize them. The comparision between original and current values will not happen and this is a more efficient mechanism. However, proxies create serialization problems, so they cannot be used in enterprise applications.
More Resources
- POCO class in Entity Framework
- POCO vs Entity Objects with comparison table
- Rules to be followed while creating POCOs - 4 important rules
- How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
- How to load related POCO entities - loading patterns and their differences in their usage with examples
- How to perform Lazy Loading with POCO classes
- What is Change tracking in POCOs, ways of tracking
- Instantiate POCO classes using ObjectContext.CreateObject method
- Using the DetectChanges() method to Fix-up Relationship in poco entities
- Entity Framework EntityKey object
- Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
- How to create an EntityKey in Entity Framework
- Loading Related entities in EF
- How to change the state of an entity using ChangeObjectState method of ObjectStateManager
- ChangeState method of ObjectStateEntry class
- Update an entity in a disconnected scenario using ApplyCurrentValues method
- Using ApplyOriginalValues method
- ObjectStateEntry class
- Entity state in Entity Framework - what, how, why?
- AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net