共用方式為


DataServiceContext.UpdateObject 方法

DataServiceContext 中指定之物件的狀態變更成 Modified

命名空間:  System.Data.Services.Client
組件:  Microsoft.Data.Services.Client (在 Microsoft.Data.Services.Client.dll 中)

語法

'宣告
Public Sub UpdateObject ( _
    entity As Object _
)
'用途
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
)

參數

例外狀況

例外狀況 條件
ArgumentNullException

當 entity 為 nullnull 參考 (在 Visual Basic 中為 Nothing) 時。

ArgumentException

當 entity 處於 Detached 狀態時。

範例

下列範例會擷取並修改現有的物件,然後呼叫 DataServiceContext 的 UpdateObject 方法,將內容中的項目標記為已更新。 呼叫 SaveChanges 時,HTTP MERGE 訊息會傳送到資料服務。 此範例使用「加入服務參考」工具所產生的 DataServiceContext,並根據完成 WCF Data Services 快速入門時所建立的 Northwind 資料服務。

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);
}

請參閱

參考

DataServiceContext 類別

System.Data.Services.Client 命名空間