【问题标题】:DocumentDB Emulator Crashes on Linq QueryDocumentDB 模拟器在 Linq 查询上崩溃
【发布时间】:2017-05-21 21:14:11
【问题描述】:

我刚刚开始使用 DocumentDB/Cosmos,遇到了一个错误,我不确定这是我做的还是错误。为了便于测试,我使用 DocumentDB 模拟器 V1.13.58.2 和 C# DocumentDB SDK V1.14.0。

一切正常,直到我尝试执行 Linq 查询,在该查询中我对 id 以外的文档属性进行相等性测试。如果我使用 id 则它可以工作,否则 DocumentDB 服务器会崩溃。我还尝试降级到 SDK 的 V1.13.4,它会抛出异常“解析值时遇到意外字符:≻.Path '', line 0, position 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 服务器崩溃。

现在所有这些都在我的云托管 Cosmos 数据库上运行良好,所以这不是一个大问题,但我只是好奇这是我正在做的事情还是只是一个错误。如果有人有任何见解,我将不胜感激。谢谢。

【问题讨论】:

    标签: c# linq emulation azure-cosmosdb


    【解决方案1】:

    我创建了一个控制台应用程序并连接到 Azure Cosmos DB Emulator 实例,我可以根据 id 属性和其他属性查询和过滤文档。

    Class 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>
    

    注意:如果跨分区执行查询,则设置EnableCrossPartitionQuery = true

    请尝试在CreateDocumentQuery&lt;TestEntityClass&gt;中使用TestEntityClass而不是CreateDocumentQuery&lt;TObject&gt;,并检查是否出现相同的错误。

    【讨论】:

    • 嗯,我在另一台电脑上试过了,对我来说也很好用。所以我然后尝试运行我的原始代码,它也可以在这台计算机上运行。我的另一台计算机上的环境必须搞砸了。感谢您的时间和帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2020-03-25
    • 2013-01-04
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    相关资源
    最近更新 更多