Your application uses the ADO.NET Entity Framework with an AdventureWorksEntities context. You create a LINQ query that retrieves customers in London ordered by company name. You want the context to compare unmodified property values with values from the data source and mark properties as modified when differences are detected. Which MergeOption setting should you use?

Difficulty: Medium

Correct Answer: context.MergeOption = MergeOption.PreserveChanges;

Explanation:


Introduction / Context:
The ADO.NET Entity Framework tracks entity state and can detect when values returned from the database differ from values currently held in memory. The MergeOption property on the ObjectContext controls how query results are merged with existing tracked entities. In some scenarios, you want the context to compare unmodified properties with store values and mark them as modified if differences are found, while still protecting properties that the client has already changed. This behavior is closely associated with the PreserveChanges merge option.


Given Data / Assumptions:

    The context is an instance of AdventureWorksEntities based on an Entity Framework model.

    You have written a LINQ to Entities query that filters customers by City == \"London\" and orders by CompanyName.

    Entities may already be in the context with some properties modified and others unmodified.

    You want a merge behavior that compares unmodified properties with store values and marks them as modified when they differ, while preserving client changes.

    You are choosing among MergeOption values such as AppendOnly, PreserveChanges, OverwriteChanges, and NoTracking.


Concept / Approach:
MergeOption.AppendOnly adds new entities to the context but does not update existing ones. MergeOption.OverwriteChanges replaces current values with store values, regardless of whether the client modified them. MergeOption.NoTracking causes entities to be returned without being tracked, so change detection does not occur. MergeOption.PreserveChanges, however, compares incoming data to existing original and current values, preserves client modifications, and can mark unmodified properties as modified if the store value differs. This matches the requirement to compare unmodified properties and flag changes that occur on the server side.


Step-by-Step Solution:
1. Before executing the query, set context.MergeOption = MergeOption.PreserveChanges; on the ObjectContext. 2. Execute the customer query that selects customers from London ordered by company name. 3. For each entity that is already tracked in the context, the framework compares the store values with the entity's current and original values. 4. If a property has not been changed on the client but differs from the store value, the context updates the property and marks it as modified to reflect that the state has changed from the original. 5. If a property has been modified on the client, PreserveChanges prevents the store value from overwriting the client value, thereby preserving local changes.


Verification / Alternative check:
You can verify this behavior by modifying some customer properties in code without saving them, then executing the query with MergeOption.PreserveChanges and checking which properties are marked as modified. You should observe that locally modified properties retain client values, while properties changed in the store are updated and flagged. Official Entity Framework documentation describes PreserveChanges as the option that preserves client changes while allowing store updates for unmodified properties.


Why Other Options Are Wrong:
AppendOnly does not refresh existing entities, so it will not detect or mark properties as modified when store values change for entities already in the context.
OverwriteChanges will override client changes with store values, which does not preserve client modifications and therefore fails the requirement.
NoTracking returns entities without attaching them to the context, so there is no change tracking or marking of modified properties.


Common Pitfalls:
Developers sometimes select AppendOnly by default and then wonder why their context never sees server side updates for entities that are already loaded. Another pitfall is using OverwriteChanges in scenarios where client modifications must be preserved, which can silently discard important user input. Choosing the appropriate MergeOption based on how you want to reconcile client and server state is essential for robust data applications.


Final Answer:
You should set context.MergeOption = MergeOption.PreserveChanges; to compare unmodified properties with store values and mark them as modified when differences are found.

More Questions from Microsoft Certification

Discussion & Comments

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