Jun 23
In this blog, we will learn how to create Azure Cosmos Database CRUD (Create, Update, Read, Delete) Operations in the employee management web application (C#).
Before starting the application, we must know: why the Azure Cosmos database?
NoSQL – Azure Cosmos DB supports NoSQL database, which makes it an easy and modern way for development.
Structure less – We can save the various type of data in a database container.
Speed – With very low latency and high availability, Microsoft gave a good SLA (Service Level Agreement) for the Cosmos DB.
Throughput – Very high throughput on all scales, and the guarantee of availability is 99.999%.
Fully managed – Similar to other services of Azure, we do not need to do the maintenance like applying patches or updates, so there is less administrative work.
Use – Nowadays, we can use this service in almost all types of applications like web, mobile, gaming, IoT and Telematics, Retail, and marketing.
Below are the steps to create a C# web application.
1. Create a sample web application named AppCosmosDBCurdOperations
2. Add a new folder name as Models and a class Employee. cs as the below code:
3. Add a new folder named Services, and Add an Interface named ICosmosDbServices.cs. Below is a sample code.
4. Create the new class CosmosDbService.cs and Implement the interface.
5. Install the required NuGet packagesAzure.Cosmos and Microsoft.EntityFrameworkCore.
6. Update the Startup.cs file using the below code.
7. Add the required configurations in AppSettings.json files as below.
8. As per the SOLID principle it is good to keep separate the service part and front-end part, but this is the demo application so we will create the front-end part in the same application. So, let’s create the folder inside the page folder with the name Employee and add three Razor pages as below:
ListEmployee.cshtml, RegisterEmployee.cshtml, UpdateEmployee.cshtml
9. And now it’s time to execute the application. Below are the screenshots after the application run.
Register the employee:
After creating the employee:
Below is from the Cosmos database:
Now, update the data.
From Cosmos DB:
After the delete button is clicked:
Record deleted from the Cosmos database:
This concludes the blog about Azure Cosmos DB. See you in the next blog!