DataServiceContext.UpdateObject(Object) メソッド

定義

DataServiceContext内の指定したオブジェクトの状態を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されたとき。

entityDetached状態のとき。

次の例では、既存のオブジェクトを取得して変更し、DataServiceContextUpdateObject メソッドを呼び出して、コンテキスト内の項目を更新済みとしてマークします。 が呼び出されると、HTTP MERGE メッセージがデータ サービスに送信されます。 この例では、Northwind データ サービスに基づいてサービス参照の追加ツールによって生成された DataServiceContext を使用します。これは、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

適用対象