Skip to content

Commit d7b2875

Browse files
authored
Merge pull request #43337 from thisisbrianstewart/master
Update Create an item section to improve coding practices, fix typo
2 parents f977492 + 71219f4 commit d7b2875

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

articles/cosmos-db/create-sql-api-dotnet.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,17 @@ private async Task AddItemsToContainerAsync()
358358
IsRegistered = false
359359
};
360360

361-
try
362-
{
363-
// Read the item to see if it exists. ReadItemAsync will throw an exception if the item does not exist and return status code 404 (Not found).
364-
ItemResponse<Family> andersenFamilyResponse = await this.container.ReadItemAsync<Family>(andersenFamily.Id, new PartitionKey(andersenFamily.LastName));
365-
Console.WriteLine("Item in database with id: {0} already exists\n", andersenFamilyResponse.Resource.Id);
366-
}
367-
catch(CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
368-
{
369-
// Create an item in the container representing the Andersen family. Note we provide the value of the partition key for this item, which is "Andersen"
370-
ItemResponse<Family> andersenFamilyResponse = await this.container.CreateItemAsync<Family>(andersenFamily, new PartitionKey(andersenFamily.LastName));
371-
372-
// Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
373-
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", andersenFamilyResponse.Resource.Id, andersenFamilyResponse.RequestCharge);
374-
}
361+
try
362+
{
363+
// Create an item in the container representing the Andersen family. Note we provide the value of the partition key for this item, which is "Andersen".
364+
ItemResponse<Family> andersenFamilyResponse = await this.container.CreateItemAsync<Family>(andersenFamily, new PartitionKey(andersenFamily.LastName));
365+
// Note that after creating the item, we can access the body of the item with the Resource property of the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
366+
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", andersenFamilyResponse.Resource.Id, andersenFamilyResponse.RequestCharge);
367+
}
368+
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.Conflict)
369+
{
370+
Console.WriteLine("Item in database with id: {0} already exists\n", andersenFamily.Id);
371+
}
375372
}
376373

377374
```

0 commit comments

Comments
 (0)