2017-05-21 74 views
0

我剛剛開始使用DocumentDB/Cosmos,並遇到錯誤,我不確定它是在做我還是在做錯誤。爲了便於測試,我使用了DocumentDB模擬器V1.13.58.2和C#DocumentDB SDK V1.14.0。DocumentDB仿真程序在Linq查詢中崩潰

一切工作正常,直到我嘗試做一個Linq查詢,我對文檔屬性以外的其他平等測試。如果我使用id,它會起作用,否則DocumentDB服務器崩潰。我也嘗試降級到SDK的V1.13.4,它會拋出一個異常「解析值時遇到意外字符:Path。路徑',第0行,第0位」。

下面是我用來創建問題的代碼。

首先我創建了一個簡單的類來使用它,然後我添加一些實例到數據庫。我可以看到使用文檔資源管理器中的正確分區成功創建了文檔。

public class TestEntityClass 
{ 
    [JsonProperty(PropertyName = "id")] 
    public Guid Id { get; set; } 
    [JsonProperty(PropertyName = "type")] 
    public int DocumentType { get; set; } 
    [JsonProperty(PropertyName = "pId")] 
    public string PartitionId { get; set; } 
    [JsonProperty(PropertyName = "stringProperty")] 
    public string StringProperty { get; set; } 
    [JsonProperty(PropertyName = "numberProperty")] 
    public int NumberProperty { get; set; } 
} 

然後我嘗試使用linq查詢數據庫,其中「match」是Linq表達式。

using (var query = m_Client.CreateDocumentQuery<TObject>(UriFactory.CreateDocumentCollectionUri(m_DBName, m_ColName), 
      new FeedOptions() { MaxItemCount = 1 }).Where(m => m.PartitionId == PartitionId && m.DocumentType == m_Type) 
      .Where(match).AsDocumentQuery()) 
     { 
      var response = await query.ExecuteNextAsync<TObject>(); 
      if (response.Count == 0) { return null; } 
      return response.ElementAt(0); 
     } 

當我設置匹配

match = m => m.Id == entity1.Id; 

它工作正常。

但是如果我設置匹配

match = m => m.NumberProperty == entity1.NumberProperty; 

match = m => m.StringProperty == entity1.StringProperty; 

的DocumentDb服務器崩潰。

現在,這一切工作正常,在我的雲中託管的宇宙數據庫,所以它不是一個巨大的問題,但我只是好奇,如果這是我在做,或只是一個錯誤。如果有人有任何見解,我將不勝感激。謝謝。

回答

0

我創建了一個控制檯應用程序,連接到Azure的宇宙模擬器DB例如,我可以查詢,並根據這兩個ID屬性和其他屬性過濾器的文件。

類TestEntityClass(如你的相同):

public class TestEntityClass 
{ 
    [JsonProperty(PropertyName = "id")] 
    public Guid Id { get; set; } 
    [JsonProperty(PropertyName = "type")] 
    public int DocumentType { get; set; } 
    [JsonProperty(PropertyName = "pId")] 
    public string PartitionId { get; set; } 
    [JsonProperty(PropertyName = "stringProperty")] 
    public string StringProperty { get; set; } 
    [JsonProperty(PropertyName = "numberProperty")] 
    public int NumberProperty { get; set; } 
} 

packages.config

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="Microsoft.Azure.DocumentDB" version="1.14.0" targetFramework="net45" /> 
    <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" /> 
</packages> 

enter image description here

:設置EnableCrossPartitionQuery = true如果跨過執行查詢分區

請嘗試在CreateDocumentQuery<TestEntityClass>中使用TestEntityClass而不是CreateDocumentQuery<TObject>,並檢查是否出現相同的錯誤。

+0

嗡嗡聲,我試過在不同的計算機上,它也適用於我。所以我然後試圖運行我的原始代碼,它也在這臺計算機上工作。在我的另一臺電腦上必須弄亂一些東西。感謝您的時間和幫助。 – Pumices

相關問題