我有以下存储过程,它仅通过 id 查找对象。
function sample(id) {
var context = getContext();
var response = context.getResponse();
var collection = context.getCollection();
var findObject = "SELECT * FROM Objects o where o.userId='" + id +"'";
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
findObject,
function (err, feed, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
if (!feed || !feed.length) throw new Error("Object not found");
else response.setBody(feed[0]);
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
这是我扩展 Document 的 C# 类
public class UserPoints: Document
{
[JsonProperty("userId")]
public Guid UserId;
[JsonProperty("remainingPoints")]
public int RemainingPoints;
}
在我的主函数中,我调用上述存储过程并期望它返回 UserId 的 UserPoints 对象。
例如:
UserPoints points = new UserPoints()
{
UserId = Guid.NewGuid(),
RemainingPoints = 1000
};
await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collection), points);
points.Dump();
var response = await client.ExecuteStoredProcedureAsync<UserPoints>(UriFactory.CreateStoredProcedureUri(databaseName, collection, storedProcedureName), points.UserId.ToString());
response.Response.Dump();
执行存储过程时,出现以下异常无法将类型为“Microsoft.Azure.Documents.Document”的对象转换为类型“UserPoints”
如果我停止扩展 Document 基类,一切都会正常,但是我无法访问更新所需的 SelfLink 属性。难道我做错了什么?如果 ExecuteStoredProcedureAsync 接受强类型,它是否应该能够对其进行类型转换并返回该类型的对象?
最佳答案
这是 DocumentDB .NET SDK 中的一个错误。我们正在研究修复方法。
请注意,作为修复程序发布之前的解决方法,您不必从 Document 派生即可将文档存储在 DocumentDB 中。如果您需要访问某些内部属性(例如 Id),您可以将这些属性添加到您的对象中,例如作为
[JsonProperty("id")] public string Id {get; set;}
关于c# - 从 .net 调用 DocumentDb 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655455/