加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:53:16 所属栏目:百科 来源:网络整理
导读:我有以下存储过程,只能通过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 +"'
我有以下存储过程,只能通过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;
}

在我的main函数中,我调用上面的存储过程并期望它返回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;}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读