Using the DetectChanges() method to Fix-up Relationship in poco entities
As you know associations in databases are bidirectional. If any modification occurs on any side of the relationship, for example changes on the master side will also reflect in the detail side and vice versa.
In Sales Order Management EDM, we have Customer and SalesOrder entities which are in one-to-many relationship. Whenever we add an order of a customer, at the same time the customer sales orders collection will be updated with the latest order. This is because, EntityObject classes know this two-way relationship and when we add an order, we can display customer details from the order for the newly added sales order.
The following code demonstrates how to add an entity to associated entities using EntityObject classes.
The ObjectContext creates a new ObjectStateEntry for the SalesOrder and its state is set as added. If you run the code, the following output is seen. If we assign sales order to a customer, we can also retrieve customer details from the sales order. This is because, when we use EntityObject classes we automatically get the two-way relationship fix-up.
The output of the code is given below.
The actual discussion is how to fix-up relationships automatically when we use POCO classes. We can do this in two ways. One is by using DetectChanges() method. The other way is by using proxies. Proxies will automatically fix the two-way relationship. In the article Change tracking with POCOs using Snapshot Change Tracking using DetectChanges() method you will learn how DetectChanges() method synchronizes the data in POCO entities and ObjectStateEntry objects. The following code demonstrates how to add an entity to associated entities using POCO classes and how relationship fix-up can be done between two entities when we add an entity to an associated entity using DetectChanges() method.
The below code defines Customer and SalesOrder POCO classes. These classes implement change tracking using snapshot mechanism and also Lazy loading capabilities. Note that if we declare only navigational properties as virtual, only lazy loading proxies are created at runtime. If we declare all the properties as virtual, change tracking proxies are created and these proxies fix the two-way relationships.
Code explanation
We create a new sales order and add to a customer whose CustomerID is 16. Comment the line ctx.DetectChanges() method and run the application. The customer data contains null because no automatic relationship fix-up is possible without DetectChanges method or without proxies. Remove the comment added earlier and run the application. The DetectChanges() method detects changes and synchronizes the data on either side of the relationship. At the end, you can call SaveChanges() method and persist the changes to the database.
In Sales Order Management EDM, we have Customer and SalesOrder entities which are in one-to-many relationship. Whenever we add an order of a customer, at the same time the customer sales orders collection will be updated with the latest order. This is because, EntityObject classes know this two-way relationship and when we add an order, we can display customer details from the order for the newly added sales order.
The following code demonstrates how to add an entity to associated entities using EntityObject classes.
The ObjectContext creates a new ObjectStateEntry for the SalesOrder and its state is set as added. If you run the code, the following output is seen. If we assign sales order to a customer, we can also retrieve customer details from the sales order. This is because, when we use EntityObject classes we automatically get the two-way relationship fix-up.
The output of the code is given below.
The actual discussion is how to fix-up relationships automatically when we use POCO classes. We can do this in two ways. One is by using DetectChanges() method. The other way is by using proxies. Proxies will automatically fix the two-way relationship. In the article Change tracking with POCOs using Snapshot Change Tracking using DetectChanges() method you will learn how DetectChanges() method synchronizes the data in POCO entities and ObjectStateEntry objects. The following code demonstrates how to add an entity to associated entities using POCO classes and how relationship fix-up can be done between two entities when we add an entity to an associated entity using DetectChanges() method.
The below code defines Customer and SalesOrder POCO classes. These classes implement change tracking using snapshot mechanism and also Lazy loading capabilities. Note that if we declare only navigational properties as virtual, only lazy loading proxies are created at runtime. If we declare all the properties as virtual, change tracking proxies are created and these proxies fix the two-way relationships.
Code explanation
We create a new sales order and add to a customer whose CustomerID is 16. Comment the line ctx.DetectChanges() method and run the application. The customer data contains null because no automatic relationship fix-up is possible without DetectChanges method or without proxies. Remove the comment added earlier and run the application. The DetectChanges() method detects changes and synchronizes the data on either side of the relationship. At the end, you can call SaveChanges() method and persist the changes to the database.
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
- Snapshot change tracking with DetectChanges() method
- Change tracking with proxies, How to create proxies?
- Instantiate POCO classes using ObjectContext.CreateObject method
- 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