简体   繁体   中英

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. Below is my documentdb code:

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 . 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.

Use 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.

Some references for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM