Friday 27 March 2020

Azure Cosmos Db Implementation with Dotnetcore and SQL Api

Simple step by step implementation of Cosmos DB with SQL Api in Dotnet core application. Official explanation could be find here.

Go to Azure Portal, search cosmos and create a subscription in specific resource group  with default/custom setting. I am using:
a. Subscription: Free Trial
b. Resource group: FreeStorageDemo
c. API: Core SQL
d. Account Name: cosmossqlapi



After successful creation of storage, get Keys:
a. URI
b. PRIMARY KEY
c. SECONDARY KEY
d. PRIMARY CONNECTION STRING
e. SECONDARY CONNECTION STRING
Mainly we will be using a and b in our case.


Either we can create Database manually through portal, or can use programming language. Here I am creating a simple dotnet core web api which will automatically create database and its containers with partition. Codebase for Cosmos db implementation with dotnet core api is available here on GitHub.

I have added Nuget package "Microsoft.Azure.Cosmos"


After running the endpoint in demo app, we can verified that Collection has been created under database in Azure portal


 And also verified that collection contains items.


Further we could fetch data by using API.



Note:
We can use _etag for the data consistency by using AccessConditionType  in RequestOption.