Quickstart: Build a Table API app with .NET and Azure Cosmos DB

This quickstart shows how to use .NET and the Azure Cosmos DB Table API to build an app by cloning an example from GitHub. This quickstart also shows you how to create an Azure Cosmos DB account and how to use Data Explorer to create tables and entities in the web-based Azure portal.

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.

Prerequisites

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

Important

You must create a new Table API account to work with the generally available Table API SDKs. Table API accounts created during preview are not supported by the generally available SDKs.

  1. In a new browser window, sign in to the Azure portal.
  2. In the left menu, click Create a resource, 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 page, enter the settings for the new Azure Cosmos DB account.

    Setting Suggested value Description
    ID Enter a unique name Enter a unique name to identify this Azure Cosmos DB account. Because documents.azure.com is appended to the ID that you provide to create your URI, use a unique but identifiable ID.

    The ID can contain only lowercase letters, numbers, and the hyphen (-) character, and it must contain 3 to 50 characters.
    API Azure Table The API determines the type of account to create. Azure Cosmos DB provides five APIs to suits the needs of your application: SQL (document database), Gremlin (graph database), MongoDB (document database), Azure Table, and Cassandra, each which currently require a separate account.

    Select Azure Table because in this quickstart you are creating a table that works with the Table API.

    Learn more about the Table API
    Subscription Enter the same unique name as provided above in ID Select Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource Group Create new

    Then enter the same unique name as provided above in ID
    Select Create New, then enter a new resource-group name for your account. For simplicity, you can use the same name as your ID.
    Location Select the region closest to your users Select geographic location in which to host your Azure Cosmos DB account. Use the location that's closest to your users to give them the fastest access to the data.
    Enable geo-redundancy Leave blank This creates a replicated version of your database in a second (paired) region. Leave this blank.
    Pin to dashboard Select Select this box so that your new database account is added to your portal dashboard for easy access.

    Then click Create.

    The new account page for Azure Cosmos DB

  4. The account creation takes a few minutes. Wait for the portal to display the Congratulations! Your Azure Cosmos DB account was created page.

    The Azure portal Notifications pane

Add a table

You can now use the Data Explorer tool in the Azure portal to create a database and table.

  1. Click Data Explorer > New Table.

    The Add Table area is displayed on the far right, you may need to scroll right to see it.

    Data Explorer in the Azure portal

  2. In the Add Table page, enter the settings for the new table.

    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 Fixed (10 GB) Use the default value of Fixed (10 GB). This value is the storage capacity of the database.
    Throughput 400 RUs Change the throughput to 400 request units per second (RU/s). If you want to reduce latency, you can scale up the throughput later.

    Click OK.

    Data Explorer displays the new database and table.

    The Azure portal Data Explorer, showing the new database and collection

Add sample data

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

  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 command prompt, create a new folder named git-samples, then close the command prompt.

    md "C:\git-samples"
    
  2. Open a git terminal window, such as git bash, and use the cd command to change to the new folder to install the sample app.

    cd "C:\git-samples"
    
  3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

    git clone https://github.com/Azure-Samples/storage-table-dotnet-getting-started.git
    

    Open the sample application in Visual Studio

  4. In Visual Studio, from the File menu, choose Open, then choose Project/Solution.

    Open the solution

  5. Navigate to the folder where you cloned the sample application, and open the TableStorage.sln file.

    Open the cloned application

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app. This enables your app to communicate with your hosted database.

  1. In the Azure portal, click Connection String.

    Use the copy button on the right side of the window to copy the PRIMARY CONNECTION STRING.

    View and copy the PRIMARY CONNECTION STRING in the Connection String pane

  2. In Visual Studio, open the App.config file.

  3. Uncomment the StorageConnectionString on line 8 and comment out the StorageConnectionString on line 7, because this tutorial does not use the Azure SDK Storage Emulator. Lines 7 and 8 should now look like this:

    <!--key="StorageConnectionString" value="UseDevelopmentStorage=true;" />-->
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=[AccountName];AccountKey=[AccountKey]" />
    
  4. Paste the PRIMARY CONNECTION STRING from the portal into the StorageConnectionString value on line 8. Paste the string inside the quotes.

    Important

    If your Endpoint uses documents.azure.com, that means you have a preview account, and you must create a new Table API account to work with the generally available Table API SDK.

    Line 8 should now appear similar to:

    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<account name>;AccountKey=<account-key>;TableEndpoint=https://<account name>.table.cosmosdb.azure.com;" />
    
  5. Press CTRL+S to save the App.config file.

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

Build and deploy the app

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

    Manage NuGet Packages

  2. In the NuGet Browse box, type Microsoft.Azure.CosmosDB.Table. This will find the Cosmos DB Table API client library. Note that this library is currently available for .NET Standard framework only, it's not yet available for .NET Core.

    NuGet Browse tab

  3. Click Install to install the Microsoft.Azure.CosmosDB.Table library. This installs the Azure Cosmos DB Table API package and all dependencies.

    Click Install

  4. Open BasicSamples.cs. Right-click on line 52, select Breakpoint, then select Insert Breakpoint. Insert another breakpoint on line 55.

    Add a breakpoint

  5. Press F5 to run the application.

    The console window displays the name of the new table database (in this case, demo91ab4) in Azure Cosmos DB.

    Console output

    If you get an error about dependencies, see Troubleshooting.

    When you hit the first breakpoint, go back to Data Explorer in the Azure portal. Click the Refresh button, expand the demo* table, and click Entities. The Entities tab on the right shows the new entity that was added for Walter Harp. Note that the phone number for the new entity is 425-555-0101.

    New entity

  6. Close the Entities tab in Data Explorer.

  7. Press F5 to run the app to the next breakpoint.

    When you hit the breakpoint, switch back to the Azure portal, click Entities again to open the Entities tab, and note that the phone number has been updated to 425-555-0105.

  8. Press F5 to run the app.

    The app adds entities for use in an advanced sample app that the Table API currently does not support. The app then deletes the table created by the sample app.

  9. In the console window, press Enter to end the execution of the app.

Review SLAs in the Azure portal

The throughput, storage, availability, latency, and consistency of the resources in your account are monitored in the Azure portal. Let's take a quick look at these metrics.

  1. Click Metrics in the navigation menu.

    Metrics in the Azure portal

  2. Click through each of the tabs so you're aware of the metrics Azure Cosmos DB provides.

    Each chart that's associated with the Azure Cosmos DB Service Level Agreements (SLAs) provides a line that shows if any of the SLAs have been violated. Azure Cosmos DB makes monitoring your SLAs transparent with this suite of metrics.

    Azure Cosmos DB metrics suite

Clean up resources

If you're not going to continue to use this app, delete all resources created by this quickstart with the following steps so you don't incur any charges:

  1. In the Azure portal, select Resource groups on the far left, and then select the resource group you created.

    If the left menu is collapsed, click Expand button to expand it.

    Metrics in the Azure portal

  2. In the new window select the resource group, and then click Delete resource group.

    Metrics in the Azure portal

  3. In the new window, type the name of the resource group to delete, 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.