Azure DocumentDB is Microsoft’s multi-tenant distributed database service for managing JSON documents at Internet scale. DocumentDB indexing enables automatic indexing of documents without requiring a schema or secondary indices. DocumentDB is designed to provide real-time consistent queries in the face of very high rates of document updates. This post is about using Azure Document db from ASP.NET Core application.
To use Document DB, first you need to create Document DB from Azure portal. You can get Azure Document DB under Data + Storage.
Once you create the Document DB Account, you can navigate to the Keys section and get the required keys, which is required in the application.
Now you completed the infrastructure configuration to use Document DB. Next you need to add the “Microsoft.Azure.DocumentDB” package in your project.json file. Here is my project.json file.
You can use DocumentClient class to execute query against your Document DB account. Here is the initialization code, which creates database and collection if not exists. I am using this code in controller constructor. And here is my controller code.
And here is CRUD application code, by default all the methods are async, so I am using async controller actions. Here is the Index action method, where I am returning all the employees in the Document DB collection.
Here is the details code, where I am getting details of an employee by Id.
And here is the code for Create, Update and Delete. Unlike SQL databases, there is no Id generated automatically, so it is better to create an Id field and assign guid values.
And here is the namespace references required.
Once you created any document in Document DB, you can view it in Azure portal, Document DB explorer feature.