You are developing a .NET application that stores user profiles in an Azure Cosmos DB SQL API container. The container's partition key path is set to `/userId`. You need to write C# code to create a new user profile document using the Cosmos DB .NET SDK v3. Which code segment should you use?
- await container.CreateItemAsync<UserProfile>(profile, new PartitionKey(profile.UserId));Answer
- Bawait client.CreateDocumentAsync(containerUri, profile, new RequestOptions { PartitionKey = new PartitionKey(profile.UserId) });
- Cawait container.CreateItemAsync<UserProfile>(profile, new PartitionKey("default"));
- Dawait container.CreateItemAsync<UserProfile>(profile, new PartitionKey(profile.UserId), new ItemRequestOptions { SessionToken = sessionToken });
Answer
await container.CreateItemAsync<UserProfile>(profile, new PartitionKey(profile.UserId));
The correct option correctly uses the .NET SDK v3 Container class method CreateItemAsync and provides the profile object along with a PartitionKey instance initialized with the UserId property, which aligns with the container's partition key path /userId.
Step-by-Step Solution
Key Concept
Creating items asynchronously in Azure Cosmos DB using the .NET SDK v3 with a partition key.