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.

automatic fix-up relationship in EntityObject classes example

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.

output of two-way relationship fix-up in EntityObject classes

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 example to fix the two-way relationships in POCO classes

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.