You are developing a .NET application for a fleet management system that logs vehicle telemetry. You need to write C# code using the Azure Cosmos DB .NET SDK v3 to insert a new status record into a container. Arrange the steps in the correct order to initialize the required SDK components and execute the item creation operation.
- 1Instantiate a new CosmosClient object by providing the Azure Cosmos DB account connection string.
- 2Obtain a Database reference by calling the GetDatabase method on the client instance.
- 3Obtain a Container reference by calling the GetContainer method on the database instance.
- 4Call the CreateItemAsync method on the container instance, passing the telemetry object and its associated PartitionKey.
Cevap
To insert an item using the Azure Cosmos DB .NET SDK v3, you must first instantiate a CosmosClient with the connection string. Using that client, call GetDatabase to get a Database reference, and then call GetContainer on the database to get a Container reference. Finally, invoke CreateItemAsync on the container reference while providing both the item object and its PartitionKey.
The Azure Cosmos DB .NET SDK v3 structures its objects in a strict hierarchy mapping directly to Azure Cosmos DB resources. You start by instantiating the CosmosClient (1), which represents the connection. From the client, you acquire a reference to the Database (2). From the database reference, you acquire a reference to the Container (3). With the Container reference, you can execute operations like CreateItemAsync (4).
Adım Adım Çözüm
Anahtar Kavram
Traversing the Azure Cosmos DB .NET SDK v3 resource hierarchy (CosmosClient to Database to Container) to perform item operations.