nerdexam
Microsoft

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…

Manipulating and Validating Data

Question

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. 3. Calls the Dispose() method on context1. 4. Updates the data in part1. 5. Updates the database by using a new ContosoEntities context object named context2. You need to update the database with the changed data from part1. What should you do?

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)
  • A
    4% (1)
  • B
    12% (3)
  • C
    81% (21)
  • D
    4% (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

#detached entity#ObjectContext#disconnected scenario#entity reattachment

Community Discussion

No community discussion yet for this question.

Full 70-516 Practice