Your application uses the ADO.NET Entity Framework with a local data store that users can modify while disconnected. When the user reconnects and retrieves data from the central store, you must preserve local changes but update unmodified entities with the latest values from the store. How should you configure the merge behavior?

Difficulty: Medium

Correct Answer: Call the query Execute method with MergeOptions.PreserveChanges.

Explanation:


Introduction / Context:
The ADO.NET Entity Framework supports offline scenarios in which entities are modified while disconnected from the central data store. When the application reconnects, it must merge new data from the store with entities already in the ObjectContext. The MergeOption and RefreshMode settings determine how conflicts and differences are resolved. In this scenario, you want local user changes to remain intact while updating unmodified entities with the latest values from the central store.


Given Data / Assumptions:

    The application uses Entity Framework with an ObjectContext and entity classes such as Customer and Order.

    Users can modify entities while disconnected, and these changes are tracked in the local context or local store.

    When reconnecting, the application runs queries to retrieve up to date data from the central database.

    Requirements are to preserve user changes in the local store and to update only unmodified entities with fresh values from the store.

    You must choose between MergeOptions values or RefreshMode settings.


Concept / Approach:
MergeOptions control how query results are merged into the ObjectContext cache. MergeOptions.AppendOnly adds new entities but does not refresh existing ones. MergeOptions.PreserveChanges compares values from the store with current values and updates only properties that have not been modified by the client, leaving local changes intact. MergeOptions.OverwriteChanges, or using StoreWins with Refresh, replaces local values with store values even if the client has modified them. ClientWins in Refresh modes tells the store to be overwritten by client values. Therefore, to update unmodified entities while preserving local changes, MergeOptions.PreserveChanges is the correct choice.


Step-by-Step Solution:
1. When setting up the query on the ObjectContext, configure its MergeOption property to MergeOptions.PreserveChanges before executing the query. 2. Execute the query to retrieve entities from the store. The context will compare incoming values with the entities already in the cache. 3. For entities that have no local modifications, the context will apply the latest store values, effectively refreshing them. 4. For entities that the user has modified while disconnected, only original or unchanged properties are updated, while modified properties are preserved. 5. This behavior satisfies both requirements: local user changes remain, and unmodified entities are updated with current data from the central store.


Verification / Alternative check:
You can verify this behavior by creating a simple test where you modify certain properties on a tracked entity, then run a query with MergeOptions.PreserveChanges set. After execution, inspect the properties: modified properties should still hold the client values, while unchanged properties reflect updated store values. Documentation for Entity Framework MergeOptions confirms this behavior.


Why Other Options Are Wrong:
MergeOptions.AppendOnly adds new entities but does not update existing ones, so unmodified entities would not receive updated values from the store.
StoreWins with RefreshMode overwrites client values with store values, which would discard local user changes and violate the requirements.
ClientWins with RefreshMode pushes client changes over store values but does not automatically pull in new store values for unchanged properties in the same way as PreserveChanges for query execution.


Common Pitfalls:
Developers sometimes use OverwriteChanges or StoreWins to simplify conflict resolution, but this can lead to data loss of client modifications. Another pitfall is misunderstanding AppendOnly and assuming it also refreshes existing entities, which it does not. Choosing the correct MergeOptions value is vital for offline capable applications that must reconcile multiple sources of truth safely.


Final Answer:
You should execute the query with MergeOptions.PreserveChanges so that local changes are preserved while unmodified entities are refreshed with the latest store values.

More Questions from Microsoft Certification

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion