Share via


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 の場合。

entity の状態が Detached の場合。

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

適用対象