共用方式為


更新資料服務 (WCF Data Services)

當您使用 WCF Data Services 用戶端程式庫來取用 Open Data Protocol (OData) 摘要時,程式庫會將摘要中的項目轉譯為用戶端資料服務類別的執行個體。您可以使用 DataServiceQuery 所屬的 DataServiceContext 來追蹤這些資料服務類別。用戶端會追蹤您使用 DataServiceContext 之方法回報的實體變更。這些方法會讓用戶端追蹤所新增及刪除的實體,以及您對屬性值所做的變更或是您對實體執行個體之間的關聯性所做的變更。當您呼叫 SaveChanges 方法時,這些追蹤的變更會當做 REST 型作業傳回資料服務。

Dd756361.note(zh-tw,VS.100).gif注意:
當您使用 DataServiceCollection 的執行個體將資料繫結至控制項時,對繫結控制項內的資料所做的變更會自動提報給 DataServiceContext。如需詳細資訊,請參閱將資料繫結至控制項 (WCF 資料服務)

新增、修改和變更實體

當您使用 Visual Studio 中的 [加入服務參考] 對話方塊,加入 OData 摘要的參考時,產生的每一個用戶端資料服務類別都有靜態 Create 方法,該方法會針對每一個不可為 null 的實體屬性採用一個參數。您可以使用這個方法建立實體類型類別的執行個體,如下列範例所示:

' Create the new product.
Dim newProduct = _
    Product.CreateProduct(0, "White Tea - loose", False)
// Create the new product.
Product newProduct =
    Product.CreateProduct(0, "White Tea - loose", false);

若要加入實體執行個體,請在 [加入服務參考] 對話方塊產生的 DataServiceContext 類別上,呼叫適當的 AddTo 方法,如下列範例所示:

' Add the new product to the Products entity set.
context.AddToProducts(newProduct)
// Add the new product to the Products entity set.
context.AddToProducts(newProduct);

如此會將物件加入內容以及正確的實體集之中。您也可以呼叫 AddObject,但是您必須提供實體集的名稱。如果加入的實體與其他實體有一個以上的關聯性,可以使用 AddRelatedObject 方法或是上述方法之一,同時也可以明確定義那些連結。這些作業稍後將在本主題中說明。

若要修改現有的實體執行個體,請先查詢該實體、針對其屬性進行所需的變更,然後呼叫 DataServiceContext 中的 UpdateObject 方法,以便讓用戶端程式庫知道它需要傳送該物件的更新,如下列範例所示:

' Mark the customer as updated.
context.UpdateObject(customerToChange)
// Mark the customer as updated.
context.UpdateObject(customerToChange);

若要刪除實體執行個體,請呼叫 DataServiceContext 中的 DeleteObject 方法,如下列範例所示:

' Mark the product for deletion.    
context.DeleteObject(deletedProduct)
// Mark the product for deletion.    
context.DeleteObject(deletedProduct);

如需詳細資訊,請參閱 HOW TO:新增、修改及刪除實體 (WCF Data Services)

附加實體

用戶端程式庫可讓您不需事先執行查詢就可以儲存您對實體所做的更新,然後將實體載入 DataServiceContext。使用 AttachTo 方法,將現有的物件附加至 DataServiceContext 中的特定實體集。然後您就可以修改物件,並將變更儲存至資料服務。在下列範例中,有一個已變更的自訂物件已附加至內容,然後在呼叫 SaveChanges 之前,先呼叫 UpdateObject,將附加的物件標示為 Modified

' Attach the existing customer to the context and mark it as updated.
context.AttachTo("Customers", customer)
context.UpdateObject(customer)

' Send updates to the data service.
context.SaveChanges()
// Attach the existing customer to the context and mark it as updated.
context.AttachTo("Customers", customer);
context.UpdateObject(customer);

// Send updates to the data service.
context.SaveChanges();

附加物件時適用下列考量:

  • 附加的物件處於 Unchanged 狀態。

  • 附加物件時,不會一併附加與該附加物件相關的物件。

  • 如果實體已依內容進行追蹤,則無法附加物件。

  • 當您附加與 eTag 值一起收到的實體物件時,會使用已採用 etag 參數的 AttachTo 方法多載。當儲存已附加之物件的變更時,這個 eTag 值可用來檢查並行存取。

如需詳細資訊,請參閱 HOW TO:將現有的實體附加至 DataServiceContext (WCF Data Services)

建立和修改關聯性連結

當您使用 AddObject 方法,或是在 [加入服務參考] 對話方塊產生的 DataServiceContext 類別上,使用適當的 AddTo 方法加入新實體,新實體與相關實體之間的關聯性不會自動定義。

您可以建立和變更實體執行個體之間的關聯性,而且可以讓用戶端程式庫在資料服務中反映那些變更。實體之間的關聯性會定義為模型中的關聯,而且 DataServiceContext 會追蹤每一個關聯性,如同內容中的連結物件。WCF Data Services 會提供 DataServiceContext 類別的下列方法,以便建立、修改和刪除這些連結:

方法 描述

AddRelatedObject

在兩個相關的實體物件之間建立新連結。呼叫這個方法相當於呼叫 AddObjectAddLink 來建立新物件並定義與現有物件的關聯性。

AddLink

在兩個相關的實體物件之間建立新連結。

SetLink

更新兩個相關實體物件之間的現有的連結。SetLink 也可以用來刪除基數為零或一對一的 (0..1:1) 和一對一 (1:1) 的連結。您可以將相關的物件設為 null 來達到這個目的。

DeleteLink

呼叫 SaveChanges 方法時,標示內容正在追蹤刪除的連結。當您刪除相關的物件或是先刪除與現有物件間的連結,然後加入與新相關物件間的連結而變更關聯性時,請使用這個方法。

AttachLink

告知兩個實體物件間之現有連結的內容。當您呼叫 SaveChanges 方法時,內容會假設這個關聯性已存在資料服務之中,而不會嘗試建立連結。當您將物件附加至內容,而且也需要附加兩者間的連結時,請使用這個方法。如果您要定義新關聯性,應改用 AddLink

DetachLink

停止追蹤內容中指定的連結。此方法用來刪除一對多 (*:*) 關聯性。對於基數為一的關聯性連結,您必須改用 SetLink

下列範例將示範如何使用 AddRelatedObject 方法,加入與現有 Orders 實體相關的新 Order_Detail。由於新的 Order_Details 物件現在是由 DataServiceContext 追蹤,因此,加入之 Order_Details 物件與現有 Products 實體間的關聯性是透過呼叫 AddLink 方法來定義:

' Add the new item with a link to the related order.
context.AddRelatedObject(order, "Order_Details", newItem)

' Since the item is now tracked by the context,
' set just the link to the related product.
context.AddLink(selectedProduct, "Order_Details", newItem)
// Add the new item with a link to the related order.
context.AddRelatedObject(order, "Order_Details", newItem);

// Since the item is now tracked by the context,
// set just the link to the related product.
context.AddLink(selectedProduct, "Order_Details", newItem);

AddLink 方法定義的連結必須在資料服務中建立時,若要將這些連結反映在內容的物件之中,您也必須設定物件本身的導覽屬性。在前面的範例中,您應設定的導覽屬性如下所示:

' Add the new order detail to the collection, and
' set the reference to the product.
order.Order_Details.Add(newItem)
newItem.Order = order
newItem.Product = selectedProduct
// Add the new order detail to the collection, and
// set the reference to the product.
order.Order_Details.Add(newItem);
newItem.Order = order;
newItem.Product = selectedProduct;

如需詳細資訊,請參閱 HOW TO:定義實體關聯性 (WCF Data Services)

儲存變更

變更會在 DataServiceContext 執行個體中追蹤,但是不會立即傳送至伺服器。在針對指定的活動完成所需的變更之後,請呼叫 SaveChanges,將所有變更提交至資料服務。如需詳細資訊,請參閱管理資料服務內容 (WCF Data Services)。您也可以透過 BeginSaveChangesEndSaveChanges 方法,非同步地儲存變更。如需詳細資訊,請參閱非同步作業 (WCF 資料服務)

另請參閱

概念

查詢資料服務 (WCF Data Services)
非同步作業 (WCF 資料服務)
批次作業 (WCF 資料服務)
物件具體化 (WCF Data Services)
管理資料服務內容 (WCF Data Services)

其他資源

WCF Data Services 用戶端程式庫