DataServiceResponse 类返回枚举操作响应后,调用 SaveChanges 时返回的结果。
继承层次结构
System.Object
System.Data.Services.Client.OperationResponse
System.Data.Services.Client.ChangeOperationResponse
命名空间: System.Data.Services.Client
程序集: Microsoft.Data.Services.Client(在 Microsoft.Data.Services.Client.dll 中)
语法
声明
Public NotInheritable Class ChangeOperationResponse _
Inherits OperationResponse
用法
Dim instance As ChangeOperationResponse
public sealed class ChangeOperationResponse : OperationResponse
public ref class ChangeOperationResponse sealed : public OperationResponse
[<SealedAttribute>]
type ChangeOperationResponse =
class
inherit OperationResponse
end
public final class ChangeOperationResponse extends OperationResponse
ChangeOperationResponse 类型公开以下成员。
属性
名称 | 说明 | |
---|---|---|
![]() |
Descriptor | 获取更改操作修改的 EntityDescriptor 或 LinkDescriptor。 |
![]() |
Error | 获取操作引发的错误。 (从 OperationResponse 继承。) |
![]() |
Headers | 在派生类中重写时,包含与单个操作相关联的 HTTP 响应标头。 (从 OperationResponse 继承。) |
![]() |
StatusCode | 在派生类中重写时,获取或设置与单个操作相关联的 HTTP 响应代码。 (从 OperationResponse 继承。) |
页首
方法
名称 | 说明 | |
---|---|---|
![]() |
Equals | (从 Object 继承。) |
![]() |
Finalize | (从 Object 继承。) |
![]() |
GetHashCode | (从 Object 继承。) |
![]() |
GetType | (从 Object 继承。) |
![]() |
MemberwiseClone | (从 Object 继承。) |
![]() |
ToString | (从 Object 继承。) |
页首
注释
ChangeOperationResponse 对象预期不由此库的用户直接构造。 相反,将在 DataServiceResponse 类的枚举器返回枚举操作响应时返回引用。
SaveChanges 会在最后一次调用 SaveChanges 时将挂起的更改提交到 DataServiceContext 收集的数据服务。 通过调用 AddObject、AddLink、DeleteObject、DeleteLink、Detach、DetachLink 和类似的方法将更改添加到上下文。
SaveChanges 返回表示对发生到数据服务的所有操作的响应的 DataServiceResponse。 DataServiceResponse 对象包括一系列 ChangeOperationResponse 对象,进而包括一系列表示已保留或尝试的更改的 EntityDescriptor 或 LinkDescriptor 实例。
示例
下面的代码演示如何处理调用 SaveChanges 所产产生的结果。
DataServiceContext service = new DataServiceContext(new Uri("http://myserviceroot"));
// Do insert, update, delete, or attach operations.
DataServiceResponse dsr;
try
{
dsr = service.SaveChanges(SaveChangesOptions.Batch);
// Or service.SaveChanges(SaveChangesOptions.ContinueOnError);
//Or service.SaveChanges();
// If there are no errors during save changes, process the results:
if (dsr.IsBatchResponse)
{
/*inspect HTTP artifacts associated with the entire batch:
dsr.BatchHeaders, dsr.BatchStatusCode*/ }
foreach (ChangeOperationResponse cor in dsr)
{
if (cor.Descriptor is EntityDescriptor)
{
EntityDescriptor ed = (EntityDescriptor)cor.Descriptor;
// This should be the case if
// SaveChanges did not throw an exception.
// After an entity is processed by SaveChanges,
// it is always moved to the unchanged state.
System.Diagnostics.Debug.Assert(
ed.State == EntityStates.Unchanged);
// This shows that the state should be unchanged if
// the result is success.
//process the entity in the response payload: ed.Entity
}
else if (cor.Descriptor is LinkDescriptor)
{
LinkDescriptor ld = (LinkDescriptor)cor.Descriptor;
// This should be the case if SaveChanges did not throw an exception.
// After an entity is processed by SaveChanges it
// is always moved to the unchanged state.
System.Diagnostics.Debug.Assert(
ld.State == EntityStates.Unchanged);
// The state should be unchanged if the result is success.
//process the link in the response payload: ld.Source,
// ld.SourceProperty, or ld.Target.
}
}
}
catch (DataServiceSaveException se)
{
// Error while saving changes
dsr = se.Response;
if (dsr.IsBatchResponse)
{
/*inspect HTTP artifacts associated with the entire batch:
dsr.BatchHeaders, dsr.BatchStatusCode*/
}
}
foreach (ChangeOperationResponse cor in dsr)
{
if (cor.Error != null)
{
//process error
}
else
{
// same success case processing as in the loop over DSRs results in
// the try block. You could put that processing in a method
// and call it from here.
}
}
}
catch(Exception)
{
// Error while saving changes, but not thrown by the client library.
// Process ArgumentException, InvalidOperationException, or similar.
}
}
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。不保证所有实例成员都是线程安全的。