DataServiceContext.UpdateObject(Object) Methode

Definition

Ändert den Zustand des angegebenen Objekts in der DataServiceContextModified

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

Parameter

entity
Object

Die nachverfolgte Entität, die dem Modified Status zugewiesen werden soll.

Ausnahmen

Wann entity ist null.

Wenn entity sich der Detached Zustand befindet.

Beispiele

Im folgenden Beispiel wird ein vorhandenes Objekt abgerufen und geändert und anschließend die UpdateObject Methode DataServiceContext aufgerufen, um das Element im Kontext als aktualisiert zu markieren. Beim SaveChanges Aufrufen wird eine HTTP MERGE-Nachricht an den Datendienst gesendet. In diesem Beispiel wird das DataServiceContext vom Tool "Dienstreferenz hinzufügen" basierend auf dem Northwind-Datendienst generierte Tool verwendet, das beim Abschließen der WCF Data Services erstellt wird.

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

Gilt für: