DataServiceContext 類別

定義

代表 DataServiceContext 資料服務的執行時上下文。

public ref class DataServiceContext
public class DataServiceContext
type DataServiceContext = class
Public Class DataServiceContext
繼承
DataServiceContext

範例

以下範例展示如何利用新增服務參考工具產生的查詢 DataServiceContext ,隱式執行對 Northwind 資料服務的查詢,回傳所有客戶。 請求 Customers 實體集合的 URI 會由上下文自動決定。 當列舉發生時,查詢會隱含執行。 Northwind 資料服務是在你完成 WCF 資料服務時建立的。

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Define a new query for Customers.
DataServiceQuery<Customer> query = context.Customers;

try
{
    // Enumerate over the query result, which is executed implicitly.
    foreach (Customer customer in query)
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a new query for Customers.
Dim query As DataServiceQuery(Of Customer) = context.Customers

Try
    ' Enumerate over the query result, which is executed implicitly.
    For Each customer As Customer In query
        Console.WriteLine("Customer Name: {0}", customer.CompanyName)
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException(
            "An error occurred during query execution.", ex)
End Try

備註

WCF 資料服務是無狀態的,但 the DataServiceContext 不是。 用戶端的狀態會在互動間維持,以支援如更新管理等功能。 這個類別,以及 DataServiceQuery 代表特定 HTTP 請求給資料服務的類別,是用戶端函式庫中的兩個主要類別。

建構函式

名稱 Description
DataServiceContext(Uri)

使用指定的 DataServiceContext,初始化 serviceRoot 類別的新實例。

屬性

名稱 Description
ApplyingChanges

會得到一個值,表示目前 DataServiceContext 是否正在對追蹤物件套用變更。

BaseUri

取得絕對 URI,識別目標資料服務的根節點。

Credentials

取得或設定每個使用該 DataServiceContext 物件所建立查詢所使用的認證資訊。

DataNamespace

取得或設定 Atom 有效載荷資料項目的 XML 命名空間,而非元資料項目。

Entities

會獲得目前所有被 .DataServiceContext

IgnoreMissingProperties

取得或設定,判斷從該型別讀取的屬性是否必須映射到用戶端型別的屬性。

IgnoreResourceNotFoundException

當資料服務回傳 404 錯誤(資源未找到)時,是否會觸發例外,取得或設定。

Links

取得物件目前追蹤 DataServiceContext 的所有關聯或連結的集合。

MergeOption

取得或設定來自資料服務的實體同步選項。

ResolveName

取得或設定一個函式,覆蓋用戶端函式庫在將實體傳送到資料服務時所使用的預設類型解析策略。

ResolveType

取得或設定一個函式,用以覆寫用戶端函式庫在接收資料服務實體時所使用的預設型別解析選項。

SaveChangesDefaultOptions

取得或設定 SaveChangesOptions 方法所使用的 SaveChanges() 值。

Timeout

取得或設定用於底層 HTTP 請求的資料服務的逾時選項。

TypeScheme

取得或設定用來指示服務所使用的型別方案的 URI。

UsePostTunneling

取得或設定一個布林值,指示是否使用後隧道。

方法

名稱 Description
AddLink(Object, String, Object)

將指定的連結加入追蹤 DataServiceContext 物件的集合中。

AddObject(String, Object)

將指定的物件加入所 DataServiceContext 追蹤的物件集合中。

AddRelatedObject(Object, String, Object)

將相關物件加入上下文,並建立連結,定義兩個物件之間的關係,形成單一請求。

AttachLink(Object, String, Object)

通知 開始 DataServiceContext 追蹤定義實體物件間關係的指定連結。

AttachTo(String, Object, String)

通知 開始 DataServiceContext 追蹤指定資源,並提供該資源在指定資源集中的位置。

AttachTo(String, Object)

通知 開始 DataServiceContext 追蹤指定資源,並提供該資源在指定資源集中的位置。

BeginExecute<T>(DataServiceQueryContinuation<T>, AsyncCallback, Object)

非同步地向資料服務發送請求,以取得分頁查詢結果中的下一頁資料。

BeginExecute<TElement>(Uri, AsyncCallback, Object)

非同步發送請求,使此呼叫不會在等待服務結果時阻塞處理。

BeginExecuteBatch(AsyncCallback, Object, DataServiceRequest[])

非同步地將一組查詢以批次形式提交給資料服務。

BeginGetReadStream(Object, DataServiceRequestArgs, AsyncCallback, Object)

非同步取得指定實體的二進位屬性資料,作為資料流,並附上指定的訊息標頭。

BeginLoadProperty(Object, String, AsyncCallback, Object)

非同步載入指定屬性的值,從資料服務中取得。

BeginLoadProperty(Object, String, DataServiceQueryContinuation, AsyncCallback, Object)

透過提供的查詢延續物件,非同步地從資料服務載入下一頁相關實體。

BeginLoadProperty(Object, String, Uri, AsyncCallback, Object)

透過提供的下一連結 URI,非同步載入資料服務中相關實體的頁面。

BeginSaveChanges(AsyncCallback, Object)

非同步地將自上次儲存以來收集 DataServiceContext 的資料服務提交待處理變更。

BeginSaveChanges(SaveChangesOptions, AsyncCallback, Object)

非同步地將自上次儲存以來收集 DataServiceContext 的資料服務提交待處理變更。

CancelRequest(IAsyncResult)

嘗試取消與所提供 IAsyncResult 物件相關的操作。

CreateQuery<T>(String)

建立指定通用型別資料的資料服務查詢。

DeleteLink(Object, String, Object)

將該連結在被追蹤 DataServiceContext連結列表中的狀態變更為已刪除。

DeleteObject(Object)

變更指定物件的狀態,該物件在 中被刪除 DataServiceContext

Detach(Object)

會將該實體從追蹤的實體 DataServiceContext 清單中移除。

DetachLink(Object, String, Object)

將指定的連結從被追蹤 DataServiceContext的連結列表中移除。

EndExecute<TElement>(IAsyncResult)

被召喚完成。BeginExecute<TElement>(Uri, AsyncCallback, Object)

EndExecuteBatch(IAsyncResult)

被召喚完成。BeginExecuteBatch(AsyncCallback, Object, DataServiceRequest[])

EndGetReadStream(IAsyncResult)

被呼叫以完成非同步操作,將二元屬性以串流形式取得。

EndLoadProperty(IAsyncResult)

被召喚完成 BeginLoadProperty(Object, String, AsyncCallback, Object) 手術。

EndSaveChanges(IAsyncResult)

被召喚完成 BeginSaveChanges(AsyncCallback, Object) 手術。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Execute<T>(DataServiceQueryContinuation<T>)

向資料服務發送請求,以取得分頁查詢結果中的下一頁資料。

Execute<TElement>(Uri)

向資料服務發送請求以執行特定的 URI。

ExecuteBatch(DataServiceRequest[])

將一組查詢以批次形式提交給資料服務。

GetEntityDescriptor(Object)

取得 supated entity 物件的 。EntityDescriptor

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetLinkDescriptor(Object, String, Object)

取得 LinkDescriptor 一個特定連結,定義兩個實體之間的關係。

GetMetadataUri()

取得 .edmx 元資料位置的 URI。

GetReadStream(Object, DataServiceRequestArgs)

取得指定實體的二進位屬性資料,作為資料流,並附上指定的訊息標頭。

GetReadStream(Object, String)

取得指定實體的二進位屬性資料,作為資料流,標頭為 Accept 訊息標頭。

GetReadStream(Object)

取得指定實體的二元屬性資料,作為資料流。

GetReadStreamUri(Object)

取得用來回傳二進位屬性資料的 URI,作為資料串流。

GetType()

取得目前實例的 Type

(繼承來源 Object)
LoadProperty(Object, String, DataServiceQueryContinuation)

利用提供的查詢延續物件載入資料服務中相關實體的下一頁。

LoadProperty(Object, String, Uri)

透過提供的下一個連結 URI 載入相關實體頁面。

LoadProperty(Object, String)

從資料服務載入指定屬性的延遲內容。

LoadProperty<T>(Object, String, DataServiceQueryContinuation<T>)

透過提供的通用查詢延續物件,從資料服務載入下一頁相關實體。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
SaveChanges()

這樣可以把他們追蹤的變更 DataServiceContext 存到儲存空間。

SaveChanges(SaveChangesOptions)

這樣可以把他們追蹤的變更 DataServiceContext 存到儲存空間。

SetLink(Object, String, Object)

通知 在 DataServiceContext 指定物件之間存在新的連結,且該連結由參數指定的 sourceProperty 屬性所代表。

SetSaveStream(Object, Stream, Boolean, DataServiceRequestArgs)

將一個新的資料串流設為實體的二進位屬性,並依請求訊息中指定的設定。

SetSaveStream(Object, Stream, Boolean, String, String)

將一個新的資料流設為實體的二進位屬性。 指定的 Content-Type 與 Slug 標頭會包含在請求訊息中。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
TryGetEntity<TEntity>(Uri, TEntity)

測試擷取被追蹤 DataServiceContext 的實體,透過參考該實體的 URI。

TryGetUri(Object, Uri)

若有,則擷取與指定實體相關的典範 URI。

UpdateObject(Object)

改變指定物件在 DataServiceContext to Modified中的狀態。

事件

名稱 Description
ReadingEntity

發生在實體資料完全讀取到實體物件後。

SendingRequest

當新角色 HttpWebRequest 被創造時,會發生這種情況。

WritingEntity

發生在實體完整序列化成 XML 後,請求訊息中出現。

適用於

另請參閱