DataServiceContext.UpdateObject(Object) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Ändrar tillståndet för det angivna objektet i DataServiceContext till Modified.
public:
void UpdateObject(System::Object ^ entity);
public void UpdateObject(object entity);
member this.UpdateObject : obj -> unit
Public Sub UpdateObject (entity As Object)
Parametrar
Undantag
När entity är null.
När entity är i tillståndet Detached .
Exempel
Följande exempel hämtar och ändrar ett befintligt objekt och anropar UpdateObject sedan metoden på DataServiceContext för att markera objektet i kontexten som uppdaterat. Ett HTTP MERGE-meddelande skickas till datatjänsten när SaveChanges det anropas. I det här exemplet används det DataServiceContext som genereras av verktyget Lägg till tjänstreferens baserat på Northwind-datatjänsten, som skapas när du slutför 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