Your application uses the ADO.NET Entity Framework to manage customers and related orders. You add a new Order entity for an existing Customer and need to associate the two entities in the object model. What should you do to correctly link the order to the customer?

Difficulty: Medium

Correct Answer: Set the Value property of the Order entity's EntityReference that points to the Customer.

Explanation:


Introduction / Context:
The ADO.NET Entity Framework models relationships between entities such as Customer and Order through navigation properties and EntityReference or EntityCollection objects. When you add a new Order for an existing Customer, you must correctly establish the relationship in the object graph so that foreign key values are set and the context understands how to persist the changes. Understanding how to use EntityReference.Value for many to one relationships is important for working effectively with Entity Framework.


Given Data / Assumptions:

    The model defines a Customer entity and an Order entity with a relationship indicating that a Customer can have many Orders.

    The Customer entity for which the new Order is being created already exists in the ObjectContext and may be attached or tracked.

    You have created a new Order entity instance in code to represent the new order.

    You must associate this new Order with the existing Customer so that Entity Framework can set the foreign key and save both entities correctly.

    Navigation properties and EntityReference or EntityCollection objects are available on the entities.


Concept / Approach:
In a typical many to one relationship, the Order entity has a reference navigation property pointing to Customer, and its underlying type is EntityReference. To associate the Order with an existing Customer, you can set the Value property of that EntityReference to the Customer instance. This tells the context that this Order belongs to that Customer and ensures that the appropriate foreign key is set when saving changes. While you can also add the Order to the Customer.Orders EntityCollection, the direct way described in many exam questions uses the EntityReference.Value property on the dependent entity.


Step-by-Step Solution:
1. Retrieve or attach the existing Customer entity to the ObjectContext so that it is tracked. 2. Create a new Order entity and set its properties, such as order date, total, and other fields. 3. Access the navigation property on the Order that represents the relationship to Customer, which is implemented as an EntityReference. 4. Set the Value property of this EntityReference to the existing Customer instance: order.CustomerReference.Value = existingCustomer;. 5. Add the Order entity to the context using AddObject if it is not already tracked, and then call SaveChanges to persist both the new Order and its association with the Customer.


Verification / Alternative check:
After calling SaveChanges, inspect the database to confirm that the new row in the Orders table has the correct foreign key value referencing the Customer row. In memory, you can also check that existingCustomer.Orders includes the new Order, demonstrating that the relationship is correctly established in both directions. Entity Framework documentation shows examples of using EntityReference.Value or adding to EntityCollection to associate related entities.


Why Other Options Are Wrong:
Calling Add on an EntityCollection of the Order entity does not make sense because Order is the dependent side and usually has a collection of OrderDetails, not Customers.
Using ObjectContext.AddObject for both Order and Customer is unnecessary if the Customer already exists and is tracked; adding it again could cause duplicate insert attempts.
Using ObjectContext.Attach for both entities is used for existing entities that are already in the database, not for a newly created Order that has not yet been saved.


Common Pitfalls:
One pitfall is trying to manually set foreign key properties without understanding navigation properties, which can lead to inconsistent object graphs. Another is forgetting to attach the existing Customer to the context before linking the new Order, which can result in errors or unexpected inserts. Using the EntityReference.Value property ties the relationship to the object model rather than to raw key values and helps Entity Framework manage state and change tracking correctly.


Final Answer:
You should set the Value property of the Order entity's EntityReference for Customer to the existing Customer entity.

More Questions from Microsoft Certification

Discussion & Comments

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