DataServiceContext.UpdateObject-Methode
Ändert den Zustand des angegebenen Objekts in Modified im DataServiceContext.
Namespace: System.Data.Services.Client
Assembly: Microsoft.Data.Services.Client (in Microsoft.Data.Services.Client.dll)
Syntax
'Declaration
Public Sub UpdateObject ( _
entity As Object _
)
'Usage
Dim instance As DataServiceContext
Dim entity As Object
instance.UpdateObject(entity)
public void UpdateObject(
Object entity
)
public:
void UpdateObject(
Object^ entity
)
member UpdateObject :
entity:Object -> unit
public function UpdateObject(
entity : Object
)
Parameter
- entity
Typ: System.Object
Die nachverfolgte Entität, die den Status Modified erhalten soll.
Ausnahmen
Ausnahme | Bedingung |
---|---|
ArgumentNullException | Wenn entity nullein NULL-Verweis (Nothing in Visual Basic) ist. |
ArgumentException | Wenn entity den Status Detached aufweist. |
Beispiele
Im folgenden Beispiel wird ein vorhandenes Objekt abgerufen und geändert und anschließend die UpdateObject-Methode im DataServiceContext aufgerufen, um das Element im Kontext als aktualisiert zu markieren. Eine HTTP MERGE-Nachricht wird an den Datendienst gesendet, wenn die SaveChanges-Methode aufgerufen wird. In diesem Beispiel wird der vom Tool erzeugte DataServiceContext auf Grundlage des Northwind-Datendiensts hinzugefügt. Dieser wird erstellt, wenn Sie den WCF Data ServicesSchnellstart durchführen.
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
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);
}