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?
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)
Strange question – D also saves the changes here. Save() internally calls DetectChanges().
You right Pavel.
In 4.5.1 SaveChanges() calls DetectChangesBeforeSave & AcceptAllChangesAfterSave
From http://referencesource.microsoft.com/#System.Data.Entity
public Int32 SaveChanges()
{
return SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave);
}
But was it the case in 4.0?
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.