c# - 从 .net 调用 DocumentDb 存储过程

标签 c# azure stored-procedures azure-cosmosdb

我有以下存储过程,它仅通过 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/

相关文章:

postgresql - Hibernate:具有递归深度搜索的存储过程:映射/输出问题

c# - 窗口最大化时获取非最大化窗口位置/大小

c# - 导入属性始终为空(MEF 导入问题)

c# - .net 中是否有任何免费的 LPR(车牌识别器)软件?

c# - 如何使用查询字符串中的数据重写 Azure 图像 URL

MySQL存储过程循环问题

c# - PetaPoco,将列表传递给存储过程

c# - 如何在asp.net应用程序中使用命名空间?

asp.net-mvc - 努力解决标准 Azure 网站中的 "Cold"代码问题

C# Azure MediaServices 上传文件引发错误