PrepAway - Latest Free Exam Questions & Answers

Which code segment should you use?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database.
You use the ADO.NET Entity Framework to manage persistence-ignorant entities. You create an ObjectContext instance named context.
Then, you directly modify properties on several entities. You need to save the modified entity values to the database.
Which code segment should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

B.
context.SaveChanges(SaveOptions.DetectChangesBeforeSave);

C.
context.SaveChanges(SaveOptions.None);

D.
context.SaveChanges();

Explanation:
None Changes are saved without the DetectChanges or the AcceptAllChangesAfterSave() methods being called.
AcceptAllChangesAfterSave After changes are saved, the AcceptAllChangesAfterSave() method is called, which resets change tracking
in the ObjectStateManager.
DetectChangesBeforeSave Before changes are saved, the DetectChanges method is called to synchronize the property values of objects
that are attached to the object context with data in the ObjectStateManager.

SaveOptions Enumeration
(http://msdn.microsoft.com/en-us/library/system.data.objects.saveoptions.aspx)

3 Comments on “Which code segment should you use?

  1. John Galt says:

    Agreed with two posters above. Both SaveChanges(SaveOptions.DetectChangesBeforeSave) and SaveChanges() and should work so both B and D should be the correct answers.

    From this link:
    http://www.vkinfotek.com/poco/acceptallchanges-savechanges-methods-in-entity-framework.html

    The default behavior of SaveChanges() method is when a transaction is executed successfully, it calls DetectChangesBeforeSave and AcceptChangesafterSave methods automatically. This automatic calling happens only if we have not passed any values to the SaveChanges() method.

    The statement ctx.SaveChanges(); is equivalent to ctx.SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptChangesAfterSave);

    Yet another shitty question on Microsoft’s part. I will be going with B on actual exam.




    0



    0

Leave a Reply