语言

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 跟踪实体。

例外

何时 entity 为 null。

何时 entity 处于 Detached 状态。

示例

以下示例检索和修改现有对象,然后在上下文中调用 UpdateObject 方法 DataServiceContext 以将上下文中的项标记为已更新。 调用数据服务时 SaveChanges ,会向数据服务发送 HTTP MERGE 消息。 此示例使用 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

适用于