ObjectContext.Refresh ObjectContext.Refresh ObjectContext.Refresh Method

Definition

Updates specific objects in the object context with data from the data source.

Overloads

Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable)

Updates a collection of objects in the object context with data from the data source.

Refresh(RefreshMode, Object) Refresh(RefreshMode, Object) Refresh(RefreshMode, Object)

Updates an object in the object context with data from the data source.

Remarks

The order in which objects are refreshed is nondeterministic.

Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable) Refresh(RefreshMode, IEnumerable)

Updates a collection of objects in the object context with data from the data source.

public:
 void Refresh(System::Data::Objects::RefreshMode refreshMode, System::Collections::IEnumerable ^ collection);
public void Refresh (System.Data.Objects.RefreshMode refreshMode, System.Collections.IEnumerable collection);
member this.Refresh : System.Data.Objects.RefreshMode * System.Collections.IEnumerable -> unit
Parameters
refreshMode
RefreshMode RefreshMode RefreshMode

A RefreshMode value that indicates whether property changes in the object context are overwritten with property values from the data source.

collection
IEnumerable IEnumerable IEnumerable

An IEnumerable collection of objects to refresh.

Exceptions

collection is empty.

-or-

An object is not attached to the context.

Remarks

This method has the dual purpose of allowing objects in the object context to be refreshed with data from the data source, and being the mechanism by which conflicts can be resolved. For more information, see Saving Changes and Managing Concurrency.

The order in which objects are refreshed is nondeterministic.

After Refresh 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 RefreshMode value. The StoreWins mode means that the objects in the collection should be updated to match the data source values. ClientWins means that only the changes in the object context will be persisted, even if there have been other changes in the data source.

To ensure that objects have been updated by data source-side logic, you can call Refresh with StoreWins after you call the SaveChanges method.

Refresh(RefreshMode, Object) Refresh(RefreshMode, Object) Refresh(RefreshMode, Object)

Updates an object in the object context with data from the data source.

public:
 void Refresh(System::Data::Objects::RefreshMode refreshMode, System::Object ^ entity);
public void Refresh (System.Data.Objects.RefreshMode refreshMode, object entity);
member this.Refresh : System.Data.Objects.RefreshMode * obj -> unit
Parameters
refreshMode
RefreshMode RefreshMode RefreshMode

One of the RefreshMode values that specifies which mode to use for refreshing the ObjectStateManager.

entity
Object Object Object

The object to be refreshed.

Exceptions

collection is empty.

-or-

An object is not attached to the context.

Examples

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 (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Perform an operation with a high-level of concurrency.
        // Change the status of all orders without an approval code.
        ObjectQuery<SalesOrderHeader> orders =
            context.SalesOrderHeaders.Where(
            "it.CreditCardApprovalCode IS NULL").Top("100");

        foreach (SalesOrderHeader order in orders)
        {
            // Reset the order status to 4 = Rejected.
            order.Status = 4;
        }
        try
        {
            // Try to save changes, which may cause a conflict.
            int num = context.SaveChanges();
            Console.WriteLine("No conflicts. " +
                num.ToString() + " updates saved.");
        }
        catch (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");
        }

        foreach (SalesOrderHeader order in orders)
        {
            Console.WriteLine("Order ID: " + order.SalesOrderID.ToString()
                + " Order status: " + order.Status.ToString());
        }
    }
    catch (UpdateException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}
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

Remarks

Refresh 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. For more information, see Saving Changes and Managing Concurrency.

The order in which objects are refreshed is nondeterministic.

After the Refresh 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 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.

To ensure that an object has been updated by data source-side logic, you can call the Refresh method with the StoreWins value after you call the SaveChanges method.

Applies to