A developer is writing a .NET application using the Azure Cosmos DB .NET SDK v3. The target container is configured with a partition key path of `/userId`. Which of the following code snippets should the developer use to retrieve a user profile item where both the item ID and the user ID are "user123"?
- await container.ReadItemAsync<UserProfile>("user123", new PartitionKey("user123"));Answer
- Bawait container.ReadItemAsync<UserProfile>("user123");
- Cawait container.ReadItemAsync<UserProfile>("user123", new PartitionKey("/userId"));
- Dawait container.ReadItemAsync<UserProfile>("user123", new PartitionKey("USA"));
Answer
await container.ReadItemAsync<UserProfile>("user123", new PartitionKey("user123"));
The correct code snippet uses the ReadItemAsync method passing both the item ID ("user123") and the PartitionKey object initialized with the partition key value ("user123") which matches the /userId path configuration.
Step-by-Step Solution
Key Concept
Performing point reads in Azure Cosmos DB using the .NET SDK v3 requires specifying both the unique item ID and its corresponding PartitionKey value.