Azure Cosmos DB: Build a .NET application using the Table API

Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.

This quick start demonstrates how to create an Azure Cosmos DB account, and create a table within that account using the Azure portal. You'll then write code to insert, update, and delete entities, and run some queries using the new Windows Azure Storage Premium Table (preview) package from NuGet. This library has the same classes and method signatures as the public Windows Azure Storage SDK, but also has the ability to connect to Azure Cosmos DB accounts using the Table API (preview).


If you don’t already have Visual Studio 2017 installed, you can download and use the free Visual Studio 2017 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.

If you don't have an Azure subscription, create a free account before you begin.

Create a database account

  1. In a new window, sign in to the Azure portal.
  2. In the left menu, click New, click Databases, and then under Azure Cosmos DB, click Create.

    Screen shot of the Azure portal, highlighting More Services, and Azure Cosmos DB

  3. In the New account blade, specify the desired configuration for the Azure Cosmos DB account.

    With Azure Cosmos DB, you can choose one of four programming models: Gremlin (graph), MongoDB, SQL (DocumentDB), and Table (key-value).

    In this quick start we'll be programming against the Table API so you'll choose Table (key-value) as you fill out the form. But if you have graph data for a social media app, document data from a catalog app, or data migrated from a MongoDB app, realize that Azure Cosmos DB can provide a highly available, globally-distributed database service platform for all your mission-critical applications.

    Fill out the New account blade using the information in the screenshot as a guide. You will choose unique values as you set up your account so your values will not match the screenshot exactly.

    Screen shot of the New Azure Cosmos DB blade

    Setting Suggested value Description
    ID Unique value A unique name you choose to identify the Azure Cosmos DB account. is appended to the ID you provide to create your URI, so use a unique but identifiable ID. The ID may contain only lowercase letters, numbers, and the '-' character, and must be between 3 and 50 characters.
    API Table (key-value) We'll be programming against the Table API later in this article.
    Subscription Your subscription The Azure subscription that you want to use for the Azure Cosmos DB account.
    Resource Group The same value as ID The new resource group name for your account. For simplicity, you can use the same name as your ID.
    Location The region closest to your users The geographic location in which to host your Azure Cosmos DB account. Choose the location closest to your users to give them the fastest access to the data.
  4. Click Create to create the account.

  5. On the toolbar, click Notifications to monitor the deployment process.

    Deployment started notification

  6. When the deployment is complete, open the new account from the All Resources tile.

    Azure Cosmos DB account on the All Resources tile

Add a table

You can now use Data Explorer to create a table and add data to your database.

  1. In the Azure portal, in the navigation menu, click Data Explorer (Preview).
  2. In the Data Explorer blade, click New Table, then fill in the page using the following information.

    Data Explorer in the Azure portal

    Setting Suggested value Description
    Table Id sample-table The ID for your new table. Table names have the same character requirements as database ids. Database names must be between 1 and 255 characters, and cannot contain / \ # ? or a trailing space.
    Storage capacity 10 GB Leave the default value. This is the storage capacity of the database.
    Throughput 400 RUs Leave the default value. You can scale up the throughput later if you want to reduce latency.
  3. Once the form is filled out, click OK.

Add sample data

You can now add data to your new table using Data Explorer (Preview).

  1. In Data Explorer, expand sample-table, click Entities, and then click Add Entity.

    Create new entities in Data Explorer in the Azure portal

  2. Now add data to the PartitionKey value box and RowKey value box, and click Add Entity.

    Set the Partition Key and Row Key for a new entity

    You can now add more entities to your table, edit your entities, or query your data in Data Explorer. Data Explorer is also where you can scale your throughput and add stored procedures, user defined functions, and triggers to your table.

Clone the sample application

Now let's clone a Table app from github, set the connection string, and run it. You'll see how easy it is to work with data programmatically.

  1. Open a git terminal window, such as git bash, and cd to a working directory.

  2. Run the following command to clone the sample repository.

    git clone
  3. Then open the solution file in Visual Studio.

Review the code

Let's make a quick review of what's happening in the app. Open the Program.cs file and you'll find that these lines of code create the Azure Cosmos DB resources.

  • The CloudTableClient is initialized.

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); 
    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
  • A new table is created if it does not exist.

    CloudTable table = tableClient.GetTableReference("people");
  • A series of steps are executed on the table using the TableOperation class.

    TableOperation insertOperation = TableOperation.Insert(item);
    TableOperation retrieveOperation = TableOperation.Retrieve<T>(items[i].PartitionKey, items[i].RowKey);
TableOperation deleteOperation = TableOperation.Delete(items[i]);

Update your connection string

Now we'll update the connection string information so your app can talk to Azure Cosmos DB.

  1. In Visual Studio, open the app.config file.

  2. In the Azure portal, in the Azure Cosmos DB left navigation menu, click Connection String. Then in the new pane click the copy button for the connection string.

    View and copy the Endpoint and Account Key in the Connection String pane

  3. Paste the value into the app.config file as the value of the PremiumStorageConnectionString.

    <add key="PremiumStorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=MYSTORAGEACCOUNT;AccountKey=AUTHKEY;TableEndpoint=" />

    You can leave the StandardStorageConnectionString as is.

You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.

Run the console app

  1. In Visual Studio, right-click on the PremiumTableGetStarted project in Solution Explorer and then click Manage NuGet Packages.

  2. In the NuGet Browse box, type WindowsAzure.Storage-PremiumTable.

  3. Check the Include prerelease box.

  4. From the results, install the WindowsAzure.Storage-PremiumTable library. This installs the preview Azure Cosmos DB Table API package as well as all dependencies. Note that this is a different NuGet package than the Windows Azure Storage package used by Azure Table storage.

  5. Click CTRL + F5 to run the application.

    The console window displays the data being added, retrieved, queried, replaced and deleted from the table. When the script completes, press any key to close the console window.

    Console output of the quickstart

  6. If you want to see the new entities in Data Explorer, just comment out lines 188-208 in program.cs so they aren't deleted, then run the sample again.

    You can now go back to Data Explorer, click Refresh, expand the people table and click Entities, and then work with this new data.

    New entities in Data Explorer

Review SLAs in the Azure portal

Now that your app is up and running, you'll want to ensure business continuity and watch user access to ensure high availability. You can use the Azure portal to review the availability, latency, throughput, and consistency of your collection.

Each graph that's associated with the Azure Cosmos DB Service Level Agreements (SLAs) provides a line that shows the quota required to meet the SLA and your actual usage. This information gives you a clear view into your database performance. Additional metrics, such as storage usage and number of requests per minute, are also included in the portal.

  • In the Azure portal, in the pane on the left, under Monitoring, select Metrics.

    Todo app with sample data

Clean up resources

If you're not going to continue to use this app, delete all resources created by this quickstart in the Azure portal with the following steps:

  1. From the left-hand menu in the Azure portal, click Resource groups and then click the name of the resource you created.
  2. On your resource group page, click Delete, type the name of the resource to delete in the text box, and then click Delete.

Next steps

In this quickstart, you've learned how to create an Azure Cosmos DB account, create a table using the Data Explorer, and run an app. Now you can query your data using the Table API.