DataServiceContext.UpdateObject(Object) Metodo

Definizione

Modifica lo stato dell'oggetto specificato in DataServiceContext in Modified.

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

Parametri

entity
Object

Entità rilevata da assegnare allo Modified stato.

Eccezioni

Quando entity è null.

Quando entity si trova nello Detached stato .

Esempio

Nell'esempio seguente viene recuperato e modificato un oggetto esistente e quindi viene chiamato il UpdateObject metodo su DataServiceContext per contrassegnare l'elemento nel contesto come aggiornato. Quando viene chiamato, viene inviato un messaggio HTTP MERGE al servizio SaveChanges dati. Questo esempio usa l'oggetto DataServiceContext generato dallo strumento Add Service Reference basato sul servizio dati Northwind, creato al termine di WCF 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

Si applica a