Soru

Zorluk: KolayPerform Container and Item Operations in Azure Cosmos DB using SDK

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.

  1. 1Instantiate a new CosmosClient object by providing the Azure Cosmos DB account connection string.
  2. 2Obtain a Database reference by calling the GetDatabase method on the client instance.
  3. 3Obtain a Container reference by calling the GetContainer method on the database instance.
  4. 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

1
Instantiate the client object
A CosmosClient instance is configured and active.
The client holds configuration settings and handles connections to the Azure Cosmos DB service.
2
Access the database reference
A Database proxy object is returned.
The SDK requires traversing the logical hierarchy: Client -> Database -> Container.
3
Access the container reference
A Container proxy object is returned.
All item-level operations such as creates, reads, and deletes are executed directly against a Container reference.
4
Execute the item creation
The telemetry item is persisted in the database.
Calling CreateItemAsync with the payload and PartitionKey sends the insert request to the correct physical partition.

Anahtar Kavram

Traversing the Azure Cosmos DB .NET SDK v3 resource hierarchy (CosmosClient to Database to Container) to perform item operations.
Bu soruyu puanla