Microsoft Exam Questions

Which code segment should you insert at line 11?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
You use the ADO.NET Entity Framework to model entities. The application connects to a Microsoft SQL Server database named AdventureWorks.
The application includes the following code segment. (Line numbers are included for reference only.)

01 using (AdventureWorksEntities context = new AdventureWorksEntities())
02 {
03 ObjectQuery <SalesOrderHeader> orders = context.SalesOrderHeader.
Where(“it.CreditCardApprovalCode IS NULL”).Top(“100”);
04 foreach (SalesOrderHeader order in orders){
05 order.Status = 4;
06 }
07 try {
08 context.SaveChanges();
09 }
10 catch (OptimisticConcurrencyException){
11 …
12 }
13 }

You need to resolve any concurrency conflict that can occur. You also need to ensure that local changes are persisted to the database.
Which code segment should you insert at line 11?

A.
context.Refresh(RefreshMode.ClientWins, orders);
context.AcceptAllChanges();

B.
context.Refresh(RefreshMode.ClientWins, orders);
context.SaveChanges();

C.
context.Refresh(RefreshMode.StoreWins, orders);
context.AcceptAllChanges();

D.
context.Refresh(RefreshMode.StoreWins, orders);
context.SaveChanges();

Explanation:
SaveChanges() Persists all updates to the data source and resets change tracking in the object context.
Refresh(RefreshMode, Object) Updates an object in the object context with data from the data source.
AcceptAllChanges() Accepts all changes made to objects in the object context.

Refresh(RefreshMode refreshMode, Object entity) Method has the dual purpose of allowing an object to be
refreshed with data from the data source and being the mechanism by which conflicts can be resolved.

ObjectContext.Refresh Method
(http://msdn.microsoft.com/en-us/library/bb896255.aspx)