DataServiceContext.UpdateObject(Object) Metoda

Definicja

Zmienia stan określonego obiektu na DataServiceContextModified.

public:
 void UpdateObject(System::Object ^ entity);
public void UpdateObject (object entity);
member this.UpdateObject : obj -> unit
Public Sub UpdateObject (entity As Object)

Parametry

entity
Object

Śledzona jednostka, która ma zostać przypisana Modified do stanu.

Wyjątki

Gdy entity jest to null.

Gdy entity jest w Detached stanie.

Przykłady

Poniższy przykład pobiera i modyfikuje istniejący obiekt, a następnie wywołuje UpdateObject metodę na obiekcie DataServiceContext , aby oznaczyć element w kontekście zgodnie z aktualizacją. Po wywołaniu komunikatu SCALANIE HTTP jest wysyłany do usługi SaveChanges danych. W tym przykładzie użyto DataServiceContext narzędzia Add Service Reference opartego na usłudze danych Northwind, która jest tworzona po zakończeniu Usługi danych programu WCF .

string customerId = "ALFKI";

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Get a customer to modify using the supplied ID.
var customerToChange = (from customer in context.Customers
                        where customer.CustomerID == customerId
                        select customer).Single();

// Change some property values.
customerToChange.CompanyName = "Alfreds Futterkiste";
customerToChange.ContactName = "Maria Anders";
customerToChange.ContactTitle = "Sales Representative";

try
{
    // Mark the customer as updated.
    context.UpdateObject(customerToChange);

    // Send the update to the data service.
    context.SaveChanges();
}
catch (DataServiceRequestException  ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}
Dim customerId = "ALFKI"

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Get a customer to modify using the supplied ID.
Dim customerToChange = (From customer In context.Customers _
                        Where customer.CustomerID = customerId _
                        Select customer).Single()

' Change some property values.
customerToChange.CompanyName = "Alfreds Futterkiste"
customerToChange.ContactName = "Maria Anders"
customerToChange.ContactTitle = "Sales Representative"

Try
    ' Mark the customer as updated.
    context.UpdateObject(customerToChange)

    ' Send the update to the data service.
    context.SaveChanges()
Catch ex As DataServiceRequestException
    Throw New ApplicationException( _
            "An error occurred when saving changes.", ex)
End Try

Dotyczy