Azure Cosmos DB: .NET examples for the SQL API
The azure-cosmos-dotnet-v2 GitHub repository includes the latest .NET sample solutions to perform CRUD and other common operations on Azure Cosmos DB resources. This article provides:
- Links to the tasks in each of the example C# project files.
- Links to the related API reference content.
For .NET SDK Version 3.0 (Preview) code samples, see the latest samples in the azure-cosmos-dotnet-v3 GitHub repository.
Visual Studio 2019 with the Azure development workflow 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.
An Azure subscription or free Cosmos DB trial account
You can activate Visual Studio subscriber benefits: Your Visual Studio subscription gives you credits every month, which you can use for paid Azure services.
You can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments. Or, you can use the Azure Cosmos DB Emulator with a URI of
https://localhost:8081. The Primary Key is provided in Authenticating requests.
The samples are self-contained, and set up and clean up after themselves with multiple calls to CreateDocumentCollectionAsync(). Each occurrence bills your subscription for one hour of usage in your collection's performance tier.
The RunDatabaseDemo method of the sample DatabaseManagement project shows how to do the following tasks. To learn about Azure Cosmos databases before you run the following samples, see Work with databases, containers, and items.
The RunCollectionDemo method of the sample CollectionManagement project shows how to do the following tasks. To learn about Azure Cosmos collections before you run the following samples, see Work with databases, containers, and items.
The RunDocumentsDemo method of the sample DocumentManagement project shows how to do the following tasks. To learn about Azure Cosmos documents before you run the following samples, see Work with databases, containers, and items.
The RunIndexDemo method of the sample IndexManagement project shows how to do the following tasks. To learn about indexing in Azure Cosmos DB before you run the following samples, see index policies, index types, and index paths.
The geospatial sample file, azure-documentdb-dotnet/samples/code-samples/Geospatial/Program.cs, shows how to do the following tasks. To learn about GeoJSON and geospatial data before you run the following samples, see Use geospatial and GeoJSON location data.
The query document file, azure-documentdb-dotnet/samples/code-samples/Queries/Program.cs, shows how to do each of the following tasks using the SQL query grammar, the LINQ provider with query, and Lambda. To learn about the SQL query reference in Azure Cosmos DB before you run the following samples, see SQL query examples for Azure Cosmos DB.
Change feed examples
The change feed sample, azure-documentdb-dotnet/samples/code-samples/ChangeFeed/Program.cs, shows how to do the following tasks. To learn about change feed in Azure Cosmos DB before you run the following samples, see Read Azure Cosmos DB change feed and Change feed processor.
|Read change feed||DocumentClient.CreateDocumentChangeFeedQuery|
|Read partition key ranges||DocumentClient.ReadPartitionKeyRangeFeedAsync|
The change feed processor sample, ChangeFeedMigrationTool, shows how to use the change feed processor library to replicate data to another Cosmos DB collection.
Server-side programming examples
The server-side programming file, azure-documentdb-dotnet/samples/code-samples/ServerSideScripts/Program.cs, shows how to do the following tasks. To learn about server-side programming in Azure Cosmos DB before you run the following samples, see Stored procedures, triggers, and user-defined functions.
User management examples
The user management file, azure-documentdb-dotnet/samples/code-samples/UserManagement/Program.cs, shows how to do the following tasks:
|Create a user||DocumentClient.CreateUserAsync|
|Set permissions on a collection or document||DocumentClient.CreatePermissionAsync|
|Get a list of a user's permissions||DocumentClient.ReadUserAsync
Send feedback about: