70-516 · Question #158
You are adding a process to the application. The process performs the following actions: 1. Opens a ContosoEntities context object named context1. 2. Loads a Part object into a variable named part1…
The correct answer is C. Add the following code segment before calling SaveChanges() on context2. How to: Apply Changes Made to a Detached Object private static void ApplyItemUpdates(SalesOrderDetail originalItem, SalesOrderDetail using (AdventureWorksEntities context = new AdventureWorksEntities()) context.SalesOrderDetails.Attach(updatedItem); // Check if the ID is 0, if…
Question
Options
- AAdd the following code segment before calling SaveChanges() on context2:
- BAdd the following code segment before calling SaveChanges() on context2:
- CAdd the following code segment before calling SaveChanges() on context2:
- DAdd the following code segment before calling SaveChanges() on context2:
How the community answered
(26 responses)- A4% (1)
- B12% (3)
- C81% (21)
- D4% (1)
Explanation
How to: Apply Changes Made to a Detached Object private static void ApplyItemUpdates(SalesOrderDetail originalItem, SalesOrderDetail using (AdventureWorksEntities context = new AdventureWorksEntities()) context.SalesOrderDetails.Attach(updatedItem); // Check if the ID is 0, if it is the item is new. // In this case we need to chage the state to Added. if (updatedItem.SalesOrderDetailID == 0) // Because the ID is generated by the database we do not need to // set updatedItem.SalesOrderDetailID. context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added); // If the SalesOrderDetailID is not 0, then the item is not new // and needs to be updated. Because we already added the // updated object to the context we need to apply the original values. // If we attached originalItem to the context // we would need to apply the current values: // context.ApplyCurrentValues("SalesOrderDetails", updatedItem); // Applying current or original values, changes the state // of the attached object to Modified. context.ApplyOriginalValues("SalesOrderDetails", originalItem); context.SaveChanges();
Topics
Community Discussion
No community discussion yet for this question.