5
我有下面的存儲過程,它只是通過id找到一個對象。從.net調用DocumentDb存儲過程
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.');
}
這是一個擴展文件
public class UserPoints: Document
{
[JsonProperty("userId")]
public Guid UserId;
[JsonProperty("remainingPoints")]
public int RemainingPoints;
}
在我的主要功能我的C#類,我調用上面的存儲過程,並希望它返回的對象UserPoints的用戶ID。
如:
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接受強類型,它不應該能夠對其進行類型轉換並返回該類型的對象?
PS。 DocumentDB中的任何操作都不需要SelfLink。您現在可以使用您嘗試引用的資源的ID。例如,ReplaceDocumentAsync(UriFactory.CreateDocumentUri(「我的數據庫ID」,「我的集合ID」,「我的文檔ID」),updated_doc_object)。有關此更多信息,請參閱https://azure.microsoft.com/zh-CN/blog/azure-documentdb-bids-fond-farewell-to-self-links/ –