DataServiceContext.UpdateObject(Object) 方法

定義

改變指定物件在 DataServiceContext to Modified中的狀態。

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

參數

entity
Object

Modified 州將被追蹤的實體。

例外狀況

entitynull

什麼 entity 時候在該 Detached 州?

範例

以下範例是檢索並修改一個現有物件,然後呼叫 UpdateObject 上的 DataServiceContext 方法,在上下文中標記該項目為已更新。 當 SaveChanges 呼叫時,會傳送 HTTP MERGE 訊息給資料服務。 此範例使用由 Add Service Reference 工具產生的, DataServiceContext 該工具基於 Northwind 資料服務,該服務會在完成 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

適用於