title | description | author | ms.service | ms.subservice | ms.devlang | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Quickstart: Table API with .NET - Azure Cosmos DB |
This quickstart shows how to use the Azure Cosmos DB Table API to create an application with the Azure portal and .NET |
SnehaGunda |
cosmos-db |
cosmosdb-table |
dotnet |
quickstart |
05/28/2020 |
sngun |
devx-track-csharp |
[!div class="op_single_selector"]
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.
If you don’t already have Visual Studio 2019 installed, you can download and use the free Visual Studio 2019 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.
[!INCLUDE quickstarts-free-trial-note]
[!INCLUDE cosmos-db-create-dbaccount-table]
[!INCLUDE cosmos-db-create-table]
[!INCLUDE cosmos-db-create-table-add-sample-data]
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.
-
Open a command prompt, create a new folder named git-samples, then close the command prompt.
md "C:\git-samples"
-
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"
-
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/azure-cosmos-table-dotnet-core-getting-started.git
Tip
For a more detailed walkthrough of similar code, see the Cosmos DB Table API sample article.
-
In Visual Studio, from the File menu, choose Open, then choose Project/Solution.
:::image type="content" source="media/create-table-dotnet/azure-cosmosdb-open-solution.png" alt-text="Open the solution":::
-
Navigate to the folder where you cloned the sample application and open the TableStorage.sln file.
This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. Otherwise, you can skip ahead to update the connection string section of this doc.
-
The following code shows how to create a table within the Azure Storage:
:::code language="csharp" source="~/azure-cosmosdb-dotnet-table/CosmosTableSamples/Common.cs" id="CreateTable":::
-
The following code shows how to insert data into the table:
:::code language="csharp" source="~/azure-cosmosdb-dotnet-table/CosmosTableSamples/SamplesUtils.cs" id="InsertItem":::
-
The following code shows how to query data from the table:
:::code language="csharp" source="~/azure-cosmosdb-dotnet-table/CosmosTableSamples/SamplesUtils.cs" id="QueryData":::
-
The following code shows how to delete data from the table:
:::code language="csharp" source="~/azure-cosmosdb-dotnet-table/CosmosTableSamples/SamplesUtils.cs" id="DeleteItem":::
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.
-
In the Azure portal, click Connection String. Use the copy button on the right side of the window to copy the PRIMARY CONNECTION STRING.
:::image type="content" source="./media/create-table-dotnet/connection-string.png" alt-text="View and copy the PRIMARY CONNECTION STRING in the Connection String pane":::
-
In Visual Studio, open the Settings.json file.
-
Paste the PRIMARY CONNECTION STRING from the portal into the StorageConnectionString value. Paste the string inside the quotes.
{ "StorageConnectionString": "<Primary connection string from Azure portal>" }
-
Press CTRL+S to save the Settings.json file.
You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.
-
In Visual Studio, right-click on the CosmosTableSamples project in Solution Explorer and then click Manage NuGet Packages.
:::image type="content" source="media/create-table-dotnet/azure-cosmosdb-manage-nuget.png" alt-text="Manage NuGet Packages":::
-
In the NuGet Browse box, type Microsoft.Azure.Cosmos.Table. This will find the Cosmos DB Table API client library. Note that this library is currently available for .NET Framework and .NET Standard.
:::image type="content" source="media/create-table-dotnet/azure-cosmosdb-nuget-browse.png" alt-text="NuGet Browse tab":::
-
Click Install to install the Microsoft.Azure.Cosmos.Table library. This installs the Azure Cosmos DB Table API package and all dependencies.
-
When you run the entire app, sample data is inserted into the table entity and deleted at the end so you won’t see any data inserted if you run the whole sample. However you can insert some breakpoints to view the data. Open BasicSamples.cs file and right-click on line 52, select Breakpoint, then select Insert Breakpoint. Insert another breakpoint on line 55.
:::image type="content" source="media/create-table-dotnet/azure-cosmosdb-breakpoint.png" alt-text="Add a breakpoint":::
-
Press F5 to run the application. The console window displays the name of the new table database (in this case, demoa13b1) in Azure Cosmos DB.
:::image type="content" source="media/create-table-dotnet/azure-cosmosdb-console.png" alt-text="Console output":::
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.
:::image type="content" source="media/create-table-dotnet/azure-cosmosdb-entity.png" alt-text="New entity":::
If you receive an error that says Settings.json file can’t be found when running the project, you can resolve it by adding the following XML entry to the project settings. Right click on CosmosTableSamples, select Edit CosmosTableSamples.csproj and add the following itemGroup:
<ItemGroup> <None Update="Settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> </ItemGroup>
-
Close the Entities tab in Data Explorer.
-
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.
-
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.
-
In the console window, press Enter to end the execution of the app.
[!INCLUDE cosmosdb-tutorial-review-slas]
[!INCLUDE cosmosdb-delete-resource-group]
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.
[!div class="nextstepaction"] Import table data to the Table API