Using Azure Cosmos DB with C#

1 hour
  • 2 Learning Objectives

About this Hands-on Lab

In this lab, you will gain experience using C# and Visual Studio to connect to and use Cosmos DB. You will RDP into a Windows VM set up with Visual Studio and update a pre-built solution with the appropriate Cosmos DB C# code to connect to Cosmos DB, upload documents, and use the SQL API to query documents. When this is complete, you will run a console application to see everything working and can go back to the Azure portal to verify the documents were saved. Upon completion of the lab, you will have gained the experience required to work with Cosmos DB using C#.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

RDP into the VM and open the Visual Studio solution.

RDP login:

UserName : "azureuser"
Password : "LA!2018!Lab"

PowerShell to run:

Add-Type -Path "C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.IO.Compression.FileSystem.dll"

$url = "https://github.com/linuxacademy/content-azure-labs/blob/master/zips/Azure-LearningActivity-Cosmos DB.zip?raw=true"
$zipfile = "C:UsersazureuserDesktopAzure-LearningActivity-Cosmos DB.zip"
$folder = "C:UsersazureuserDesktop"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -UseBasicParsing -OutFile $zipfile $url 

[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $folder)

Remove-Item -Path $zipfile 
Update the Visual Studio solution and run the console app.

Create a New Container in the Azure Portal

  1. In the Azure portal, click All resources.
  2. Select the Azure Cosmos DB account resource in the list.
  3. Click Add Container.
  4. In the Add Container pane, set the following values:
    • Database id: Create new, and name it db1
    • Container id: collection1
    • Partition key: /Partition
    • Throughput: 400
  5. Click OK.

Update the Visual Studio Code on VM

  1. Back on the virtual machine, in the Visual Studio window, double-click Program.cs in the right-hand menu to open it.
  2. In the Program class, we need to enter a couple pieces of missing information: EndpointUrl and PrimaryKey. In the Azure Portal, head to the Overview section for our Cosmos DB account.
  3. Copy the listed URI.
  4. Back in Visual Studio, paste it in as the EndpointUrl value.
  5. In the Azure portal, scroll down and click Keys (under the Settings heading).
  6. Copy the listed primary key.
  7. Back in Visual Studio, paste it in as the PrimaryKey value.
  8. Click the disk icon to save the code.
  9. Click the play button icon to run the console app.
  10. Back in the Azure portal, scroll down and click Document Explorer (under the Containers heading).
  11. Click Open Data Explorer.
  12. Click collection1.
  13. Click Documents. We should see two documents listed, which means we were successful.

Additional Resources

As a developer, you want to use the Azure Cosmos DB SDK and C# code to connect to and interact with Cosmos DB.

To do this, you will RDP into a virtual machine in Azure, download a pre-built solution, and fill in the correct C# code to get the program to compile. When you run the program, it will save and retrieve documents from Cosmos DB.

You are given step-by-step instructions in the activity tasks for this lab. Follow along with those and you will have successfully connected to and interacted with Cosmos DB.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!