DataServiceContext.UpdateObject(Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 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
为 null
时。
当 entity
处于 Detached 状态时。
示例
下面的示例检索和修改现有的对象,然后对 UpdateObject 调用 DataServiceContext 方法以将上下文中的项标记为已更新。 调用 SaveChanges 时,HTTP MERGE 消息将会发送到数据服务。 此示例使用DataServiceContext基于 Northwind 数据服务的“添加服务引用”工具生成的 ,该服务是在完成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