DataServiceContext.UpdateObject(Object) Método

Definición

Cambia el estado del objeto especificado en DataServiceContext a Modified.Changes the state of the specified object in the DataServiceContext to Modified.

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

Parámetros

entity
Object

Entidad de la que se realiza un seguimiento y que va a asignarse al estado Modified.The tracked entity to be assigned to the Modified state.

Excepciones

Cuando entity es null.When entity is null.

Cuando entity se encuentra en el estado Detached.When entity is in the Detached state.

Ejemplos

En el siguiente ejemplo se recupera y modifica un objeto existente y, a continuación, se llama al método UpdateObject en DataServiceContext para marcar el elemento en el contexto cuando se actualice.The following example retrieves and modifies an existing object and then calls the UpdateObject method on the DataServiceContext to mark the item in the context as updated. Cuando se llama a SaveChanges se envía un mensaje HTTP MERGE al servicio de datos.An HTTP MERGE message is sent to the data service when SaveChanges is called. En este ejemplo se utiliza el DataServiceContext generado por la herramienta de Agregar referencia de servicio basada en el servicio de datos Northwind, que se crea al completar el Servicios de datos de WCFWCF Data Services.This example uses the DataServiceContext generated by the Add Service Reference tool based on the Northwind data service, which is created when you complete the Servicios de datos de WCFWCF Data Services .

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

Se aplica a