[英]Create document not working - C# DocumentDB/CosmosDB
I think I am on the right path, but my C# code is not creating document in Azure CosmosDB. 我认为我在正确的道路上,但我的C#代码不是在Azure CosmosDB中创建文档。 Below is my documentdb code: 下面是我的documentdb代码:
using (var client = new DocumentClient(new Uri(endpoint), authkey))
{
Database database = client.CreateDatabaseQuery("SELECT * FROM c WHERE c.id = 'db'").AsEnumerable().First();
var query = new SqlQuerySpec
{
QueryText = "SELECT * FROM c WHERE c.id = @id",
Parameters = new Microsoft.Azure.Documents.SqlParameterCollection { new Microsoft.Azure.Documents.SqlParameter { Name = "@id", Value = collectionId }
}
};
DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink,query).AsEnumerable().First();
dynamic document1Definition = new
{
name = "Admin",
address = 1,
};
var result = client.CreateDocumentAsync(collection.SelfLink, document1Definition);
}
Also want to point out, currently there are no columns named as "name" and "address" in my collection . 还想指出,目前在我的集合中没有名为“name”和“address”的列 。 So according to my knowledge they are suppose to be created dynamically. 所以根据我的知识,他们假设是动态创建的。 Please let me know what wrong are you doing? 请告诉我你做错了什么?
See last statement, you are using c# Async method without await. 请参阅最后一条语句,您正在使用c#Async方法而不等待。
Use await client.CreateDocumentAsync(collection.SelfLink, document1Definition);
使用await client.CreateDocumentAsync(collection.SelfLink, document1Definition);
or your code will exit before document creation is finished. 或者您的代码将在文档创建完成之前退出。
Note that your method should change to public async Task methodname()
, you will see related tip shown by VS. 请注意,您的方法应更改为public async Task methodname()
,您将看到VS显示的相关提示。
Some references for you 一些参考资料
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.