This folder contains a C# (.NET) sample that uses the Azure Resource Manager (ARM) / Management Plane SDKs to create and update Azure Cosmos DB resources.
This is useful when your application uses Microsoft Entra ID and you want to manage Cosmos DB resources (accounts, databases, containers, throughput, and RBAC) via an SDK instead of Bicep/PowerShell/Azure CLI.
Important: This sample uses the control plane (resource provider) APIs via ARM. It does not use the Cosmos DB data plane SDK (
Microsoft.Azure.Cosmos) to create ARM resources.
- Create or update a Cosmos DB SQL (NoSQL) account.
- Sets
DisableLocalAuth = true(disables key-based auth; Entra ID + RBAC required). - Enables the
EnableNoSQLVectorSearchcapability. - Leaves a serverless capability example commented out.
- Create or update a SQL database.
- Create or update a SQL container with:
- Hierarchical partition key (multi-hash) on
/companyId,/departmentId,/userId. - Indexing policy (consistent), plus a vector index on
/vectors. - Vector embedding definition (1536 dims, cosine distance).
- Unique key on
/userId. - Computed property example.
- TTL enabled with no default (container
DefaultTtl = -1). - Last-writer-wins conflict resolution (
/_ts). - Autoscale max throughput from configuration.
- Hierarchical partition key (multi-hash) on
- Updates container dedicated throughput by reading current settings first and then:
- Updating autoscale max throughput when the container is autoscale, or
- Updating RU/s when the container is manual throughput.
- Re-reads and logs the applied settings after the update.
- Throws a clear error when the throughput resource doesn’t exist (common for serverless accounts or shared database throughput).
This sample creates two role assignments by default for the currently signed-in principal:
- Azure RBAC (control plane): assigns the built-in
Cosmos DB Operatorrole to the Cosmos account scope. - Cosmos DB SQL RBAC (data plane): assigns the built-in
Cosmos DB Built-in Data Contributorrole.
It also includes a custom Cosmos DB SQL RBAC role definition example (not used by default).
- Runs an interactive menu by default.
- Includes a "Run full sample" menu option.
- Supports deleting the Cosmos DB account:
- From the menu (requires typing
DELETEto confirm) - From the full run only when
COSMOS_SAMPLE_DELETE_ACCOUNT=true(opt-in safety guard)
- From the menu (requires typing
- An Azure subscription and a resource group.
- .NET 10 SDK (LTS).
- Azure identity available to
DefaultAzureCredential(e.g.,az login, VS Code sign-in, Managed Identity, etc.). - Permissions:
- To create/update Cosmos resources: typically Contributor on the resource group.
- To create Azure RBAC role assignments: typically Owner or User Access Administrator at the target scope.
Configuration is bound to AppSettings from appsettings.json / environment-specific settings.
Keys:
SubscriptionIdResourceGroupNameAccountNameLocationDatabaseNameContainerNameMaxAutoScaleThroughput
Notes:
Host.CreateDefaultBuilder()loadsappsettings.jsonandappsettings.{Environment}.json.- This sample defaults
DOTNET_ENVIRONMENTtoDevelopmentif it isn't already set (soappsettings.development.jsonis used automatically). - Set
DOTNET_ENVIRONMENTexplicitly if you want to override (for example,Production).
From the repo root:
cd Csharp
dotnet restore CosmosManagement.csprojUpdate configuration in appsettings.json (or run with DOTNET_ENVIRONMENT=Development and update appsettings.development.json).
cd Csharp
dotnet runRun dotnet run, then choose "Run full sample" from the menu.
Run dotnet run, then choose "Run full sample" from the menu.
Windows (PowerShell):
$env:COSMOS_SAMPLE_DELETE_ACCOUNT = "true"
dotnet runmacOS/Linux:
export COSMOS_SAMPLE_DELETE_ACCOUNT=true
dotnet runTo debug without requiring all language extensions in this multi-language repo, open the workspace file Csharp.code-workspace and use the launch configuration “C#: Debug sample”.
- Sets
DOTNET_ENVIRONMENT=Development - The launch configuration's
programshould point to${workspaceFolder}/bin/Debug/net10.0/CosmosManagement.dll.
Use the Run and Debug panel to start it.
- Throughput update fails with “settings not found”: the container likely uses shared database throughput or the account is serverless. Create the container with dedicated throughput (or update database throughput instead).
- RBAC assignment fails: your identity likely lacks
Microsoft.Authorization/roleAssignments/writeat the chosen scope. - Options validation fails on startup: one or more required config values are missing/empty.