Share via


DataServiceContext.UpdateObject(Object) Méthode

Définition

Remplace l'état de l'objet spécifié dans le DataServiceContext par Modified.

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

Paramètres

entity
Object

Entité suivie à assigner à l'état Modified.

Exceptions

Lorsque entity est null.

Lorsque entity est à l'état Detached.

Exemples

L'exemple suivant récupère et modifie un objet existant puis appelle la méthode UpdateObject sur DataServiceContext pour marquer l'élément dans le contexte comme mis à jour. Un message HTTP MERGE est envoyé au service de données lors de l’appel de SaveChanges. Cet exemple utilise le DataServiceContext généré par l’outil Ajouter une référence de service basé sur le service de données Northwind, qui est créé lorsque vous terminez la 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

S’applique à