This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
In this quickstart, you deploy a basic Azure Cosmos DB for Table application using the Azure SDK for .NET. Azure Cosmos DB for Table is a schemaless data store allowing applications to store structured table data in the cloud. You learn how to create tables, rows, and perform basic tasks within your Azure Cosmos DB resource using the Azure SDK for .NET.
API reference documentation |Library source code |Package (NuGet) |Azure Developer CLI
If you don't have an Azure account, create afree account before you begin.
Use the Azure Developer CLI (azd) to create an Azure Cosmos DB for Table account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
Open a terminal in an empty directory.
If you're not already authenticated, authenticate to the Azure Developer CLI usingazd auth login. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.
azd auth loginUseazd init to initialize the project.
azd init --template cosmos-db-table-dotnet-quickstartDuring initialization, configure a unique environment name.
Deploy the Azure Cosmos DB account usingazd up. The Bicep templates also deploy a sample web application.
azd upDuring the provisioning process, select your subscription, desired location, and target resource group. Wait for the provisioning process to complete. The process can takeapproximately five minutes.
Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
Deploying services (azd deploy) (✓) Done: Deploying service web- Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io>SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.

The client library is available through NuGet, as theAzure.Data.Tables package.
Open a terminal and navigate to the/src/web folder.
cd ./src/webIf not already installed, install theAzure.Data.Tables package usingdotnet add package.
dotnet add package Azure.Data.TablesOpen and review thesrc/web/Microsoft.Samples.Cosmos.Table.Quickstart.Web.csproj file to validate that theAzure.Data.Tables entry exists.
Import theAzure.Identity andAzure.Data.Tables namespaces into your application code.
using Azure.Identity;using Azure.Data.Tables;| Name | Description |
|---|---|
| TableServiceClient | This class is the primary client class and is used to manage account-wide metadata or databases. |
| TableClient | This class represents the client for a table within the account. |
The sample code in the template uses a table namedcosmicworks-products. Thecosmicworks-products table contains details such as name, category, quantity, price, a unique identifier, and a sale flag for each product. The container uses aunique identifier as the row key andcategory as a partition key.
This sample creates a new instance of theTableServiceClient class.
DefaultAzureCredential credential = new();TableServiceClient serviceClient = new( endpoint: new Uri("<azure-cosmos-db-table-account-endpoint>"), credential);This sample creates an instance of theTableClient class using theGetTableClient method of theTableServiceClient class.
TableClient client = serviceClient.GetTableClient( tableName: "<azure-cosmos-db-table-name>");The easiest way to create a new entity in a table is to create a class that implements theITableEntity interface. You can then add your own properties to the class to populate columns of data in that table row.
public record Product : ITableEntity{ public required string RowKey { get; set; } public required string PartitionKey { get; set; } public required string Name { get; set; } public required int Quantity { get; set; } public required decimal Price { get; set; } public required bool Clearance { get; set; } public ETag ETag { get; set; } = ETag.All; public DateTimeOffset? Timestamp { get; set; }};Create an entity in the table using theProduct class by callingTableClient.AddEntityAsync<T>.
Product entity = new(){ RowKey = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb", PartitionKey = "gear-surf-surfboards", Name = "Surfboard", Quantity = 10, Price = 300.00m, Clearance = true};Response response = await client.UpsertEntityAsync<Product>( entity: entity, mode: TableUpdateMode.Replace);You can retrieve a specific entity from a table using theTableClient.GetEntityAsync<T> method. Provide thepartitionKey androwKey as parameters to identify the correct row to perform a quickpoint read of that entity.
Response<Product> response = await client.GetEntityAsync<Product>( rowKey: "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb", partitionKey: "gear-surf-surfboards");After you insert an entity, you can also run a query to get all entities that match a specific filter by using theTableClient.Query<T> method. This example filters products by category using Language Integrated Query (LINQ) syntax, which is a benefit of using typedITableEntity models like theProduct class.
string category = "gear-surf-surfboards";AsyncPageable<Product> results = client.QueryAsync<Product>( product => product.PartitionKey == category);Parse the paginated results of the query by looping through each page of results using asynchronous loop.
List<Product> entities = new();await foreach (Product product in results){ entities.Add(product);}When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd downWas this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?