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
)
参数
- entity
类型:System.Object
将指定为 Modified 状态的被跟踪实体。
异常
异常 | 条件 |
---|---|
ArgumentNullException | 当 entity 为 nullnull 引用(在 Visual Basic 中为 Nothing) 时。 |
ArgumentException | 当 entity 处于 Detached 状态时。 |
示例
下面的示例检索和修改现有的对象,然后对 DataServiceContext 调用 UpdateObject 方法以将上下文中的项标记为已更新。 调用 SaveChanges 时,HTTP MERGE 消息将会发送到数据服务。 此示例使用基于 Northwind 数据服务(在您完成 WCF 数据服务?快速入门时创建)的“添加服务引用”工具所生成的 DataServiceContext。
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);
}