DocumentDB 模拟器在 Linq 查询时崩溃
DocumentDB Emulator Crashes on Linq Query
我刚开始使用 DocumentDB/Cosmos 并遇到 运行 错误,我不确定这是我做的还是错误。为了便于测试,我使用的是 DocumentDB 模拟器 V1.13.58.2 和 C# DocumentDB SDK V1.14.0.
一切正常,直到我尝试执行 Linq 查询,在该查询中我对 id 以外的文档 属性 进行相等性测试。如果我使用 id 它会工作,否则 DocumentDB 服务器会崩溃。我也尝试降级到 SDK 的 V1.13.4,它抛出异常 "Unexpected character encountered while parsing value: ≻. Path '', line 0, position 0".
下面是我用来创建问题的代码。
首先,我创建了一个简单的 class 来使用它,然后将一些实例添加到数据库中。我可以在文档资源管理器中看到文档已使用正确的分区成功创建。
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 数据库上运行良好,所以这不是一个大问题,但我只是好奇这是我在做的事情还是只是一个错误。如果有人有任何见解,我将不胜感激。谢谢。
我创建了一个控制台应用程序并连接到 Azure Cosmos DB 模拟器实例,我可以根据 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<TestEntityClass>
中使用TestEntityClass
而不是CreateDocumentQuery<TObject>
,并检查是否出现相同的错误。
我刚开始使用 DocumentDB/Cosmos 并遇到 运行 错误,我不确定这是我做的还是错误。为了便于测试,我使用的是 DocumentDB 模拟器 V1.13.58.2 和 C# DocumentDB SDK V1.14.0.
一切正常,直到我尝试执行 Linq 查询,在该查询中我对 id 以外的文档 属性 进行相等性测试。如果我使用 id 它会工作,否则 DocumentDB 服务器会崩溃。我也尝试降级到 SDK 的 V1.13.4,它抛出异常 "Unexpected character encountered while parsing value: ≻. Path '', line 0, position 0".
下面是我用来创建问题的代码。
首先,我创建了一个简单的 class 来使用它,然后将一些实例添加到数据库中。我可以在文档资源管理器中看到文档已使用正确的分区成功创建。
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 数据库上运行良好,所以这不是一个大问题,但我只是好奇这是我在做的事情还是只是一个错误。如果有人有任何见解,我将不胜感激。谢谢。
我创建了一个控制台应用程序并连接到 Azure Cosmos DB 模拟器实例,我可以根据 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<TestEntityClass>
中使用TestEntityClass
而不是CreateDocumentQuery<TObject>
,并检查是否出现相同的错误。