ObjectContext.Refresh Method (RefreshMode, Object)
Updates an object in the object context with data from the data source.
Assembly: System.Data.Entity (in System.Data.Entity.dll)
The order in which objects are refreshed is nondeterministic.
After the RefreshMode. The StoreWins mode means that the object should be updated to match the data source values. The ClientWins value means that only the changes in the object context will be persisted, even if there have been other changes in the data source.method is called, the object’s original values will always be updated with the data source value, but the current values might or might not be updated with the data source value. This depends on the
This example is based on the AdventureWorks Sales Model. The example tries to save changes, and this may cause a concurrency conflict. Then it shows how to resolve the concurrency conflict by refreshing the object context before re-saving changes.
Using context As New AdventureWorksEntities() Try ' Perform an operation with a high-level of concurrency. ' Change the status of all orders without an approval code. Dim orders As ObjectQuery(Of SalesOrderHeader) = context.SalesOrderHeaders.Where("it.CreditCardApprovalCode IS NULL").Top("100") For Each order As SalesOrderHeader In orders ' Reset the order status to 4 = Rejected. order.Status = 4 Next Try ' Try to save changes, which may cause a conflict. Dim num As Integer = context.SaveChanges() Console.WriteLine("No conflicts. " & num.ToString() & " updates saved.") Catch generatedExceptionName As OptimisticConcurrencyException ' Resolve the concurrency conflict by refreshing the ' object context before re-saving changes. context.Refresh(RefreshMode.ClientWins, orders) ' Save changes. context.SaveChanges() Console.WriteLine("OptimisticConcurrencyException handled and changes saved") End Try For Each order As SalesOrderHeader In orders Console.WriteLine(("Order ID: " & order.SalesOrderID.ToString() & " Order status: ") + order.Status.ToString()) Next Catch ex As UpdateException Console.WriteLine(ex.ToString()) End Try End Using
Available since 3.5