Azure Cosmos DB – SQL API Geo-Replication Utilizing EF Core

Within the earlier article, now we have seen the introduction of Cosmos DB and EF Core with Cosmos DB-SQL API. When you haven’t learn my earlier articles then I extremely encourage you to take action:

Geo-replication will be enabled by clicking on “Allow Geo Replication”.

On this instance, I’ve configured East US 2, North Europe and SouthEast Asia area for reads and writes.



As in comparison with the earlier article, I’m implementing the identical program in Internet API fairly than the console App. I’ll not be explaining the code till it’s completely needed.


You’ll be able to undergo my earlier article by referring to the recap part.

 Now, register the ProfileContext within the ConfigureServices methodology of Startup class

Now run the appliance and it really works! However wait, as now we have configured geo-replication to which area of database occasion the appliance known as? Good query.


To confirm this we will allow Utility Perception in our software by including a nuget bundle Microsoft.ApplicationInsights.AspNetCore


Register Utility Perception within the ConfigureServices methodology of Startup class.

  1. providers.AddApplicationInsightsTelemetry();  
  1. “ApplicationInsights”: {  
  2.         “InstrumentationKey”“<Instrumentation Key right here>”  
  3.     },  

 You will get the instrumentation key by executing under command

  1. # Shows Instrumentation key   
  2. az useful resource present -g $resourceGroup -n   
  3. $title –resource-type “Microsoft.Insights/elements”  
  4. –query properties.InstrumentationKey  

Now deploy your software to Azure app service both to “AppServiceSI” or “AppServiceEU”. You’ll be able to navigate to Utility Perception by click on on the useful resource group (GeoReplication) and now click on on Utility Map. Now exactly, it is possible for you to to see on which database occasion the question has been executed. 


As , now we have 2 App Service

  • AppServiceSI- Deployed in South India
  • AppServiceEU- Deployed in East US

Now the appliance executes randomly on each the situations of the database. However is there a approach to set the prefered location of the database? ex: App service deployed in South India ought to level to SouthEast Asia database area whereas, the app service deployed in East US ought to level to East US 2 database area as a result of these database situations are geographically nearer to the app service location.


I want to thank Julie Lerman, Jeremy Likness, and Smit Patel for giving the reply to configure the popular location. I used to be struggling to get the reply and there are not any articles/questions round most well-liked location wherever.




You’ll be able to configure the popular area within the AddDbContext methodology:

  1. public void ConfigureServices(IServiceCollection providers)  
  2.        {  
  3.            var accountEndpoint = Configuration.GetValue<string>(“ConnectionString:AccountEndpoint”);  
  4.            var accountKey= Configuration.GetValue<string>(“ConnectionString:AccountKey”);  
  5.            var dbName= Configuration.GetValue<string>(“ConnectionString:DatabaseName”);  
  6.            var area = Configuration.GetValue<string>(“ApiServerRegion”);   
  7.            providers.AddDbContext<ProfileContext>(x => x.UseCosmos(accountEndpoint, accountKey, dbName  
  8. ,cosmosOptionsAction=>cosmosOptionsAction.Area(area)));  
  9.            providers.AddControllers();  
  10.            providers.AddApplicationInsightsTelemetry();  
  11.        }  

 Within the appsettings.json, add the under strains for East US App service whereas, set the ApiServerRegion=”SouthEastAsia” for the South Indian App service.

  1. “ApiServerRegion”“EastUS2”,  

Now deploy each the app service by setting the correct ApiServerRegion within the appsettings.json.


Now run each the app providers. It is possible for you to to see the outcome within the software map current in Utility Perception.


I hope you just like the article. In case you discover the article as fascinating then kindly like and share it.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button