C# 从.net调用DocumentDb存储过程
我有下面的存储过程,它只是通过id查找对象C# 从.net调用DocumentDb存储过程,c#,azure,stored-procedures,azure-cosmosdb,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 +"'"; // Qu
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.');
}
这是我扩展文档的C#类
public class UserPoints: Document
{
[JsonProperty("userId")]
public Guid UserId;
[JsonProperty("remainingPoints")]
public int RemainingPoints;
}
在我的主函数中,我调用上述存储过程,并期望它返回UserId的UserPoints对象
例如:
UserPoints=newuserpoints()
{
UserId=Guid.NewGuid(),
剩余点数=1000
};
等待client.createDocumentSync(UriFactory.CreateDocumentCollectionUri(databaseName,collection),points);
points.Dump();
var response=wait client.ExecuteStoredProcedureAsync(UriFactory.CreateStoredProcedureUri(databaseName,collection,storedProcedureName),points.UserId.ToString());
response.response.Dump();
执行存储过程时,我遇到以下异常无法将类型为“Microsoft.Azure.Documents.Document”的对象强制转换为类型为“UserPoints”
如果我停止扩展文档基类,那么一切都可以正常工作,但是我无法访问更新所需的SelfLink属性。我做错什么了吗?如果ExecuteStoreProcedureAsync采用强类型,它是否应该能够对其进行类型转换并返回该类型的对象?这是DocumentDB.NET SDK中的一个错误。我们正在调查一个修复方案 请注意,在发布修复程序之前,作为一种变通方法,您不必从文档派生来将文档存储在DocumentDB中。如果需要访问某些内部属性(如Id),可以将这些属性添加到对象中,例如
[JsonProperty("id")] public string Id {get; set;}
另外,DocumentDB中的任何操作都不再需要SelfLink。您现在可以只使用您试图引用的资源的id。例如,replaceDocumentSync(UriFactory.CreateDocumentUri(“我的数据库id”、“我的集合id”、“我的文档id”)、更新的文档对象)。有关这方面的更多信息,请参阅
[JsonProperty("id")] public string Id {get; set;}