Bagikan melalui


DataServiceContext.UpdateObject(Object) Metode

Definisi

Mengubah status objek yang ditentukan di ke 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

Entitas terlacak yang akan ditetapkan ke status Modified .

Pengecualian

Kapan entity adalah null.

Ketika entity berada di negara bagian Detached .

Contoh

Contoh berikut mengambil dan memodifikasi objek yang ada lalu memanggil UpdateObject metode pada DataServiceContext untuk menandai item dalam konteks sebagai diperbarui. Pesan HTTP MERGE dikirim ke layanan data saat SaveChanges dipanggil. Contoh ini menggunakan DataServiceContext yang dihasilkan oleh alat Tambahkan Referensi Layanan berdasarkan layanan data Northwind, yang dibuat saat Anda menyelesaikan Layanan Data WCF .

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

Berlaku untuk