You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been using Cosmos for some time and recently moved over to use Microsoft.Azure.Documents.Client. I found the documentation pretty easy to get going until I tried to delete documents.
I realise that my mistake is somewhat silly, but possibly a couple of comments would have saved me a few hours.
In the sample for deletions:
private static async Task DeleteDocumentAsync()
{
Console.WriteLine("\n1.7 - Deleting a document");
ResourceResponse<Document> response = await client.DeleteDocumentAsync(
UriFactory.CreateDocumentUri(databaseName, collectionName, "SalesOrder3"),
new RequestOptions { PartitionKey = new PartitionKey("Account1") });
Console.WriteLine("Request charge of delete operation: {0}", response.RequestCharge);
Console.WriteLine("StatusCode of operation: {0}", response.StatusCode);
}
I wrongly interpretted the PartitionKey to be the PartitionKey set on the container, in my case VesselID, when it should have been the value of the VesselID, in my case an int like 123.
A couple of suggestions to make this more obvious.
If the DeleteDocumentAsync method took two inputs, one for partitionKey and one for the documentID, it would be more obvious. The calling code would show the paritionKey which would have made it a little more easy to follow
A comment to say that it was the value of the PartitionKey.
In my own code, I ended up with, it could obviously be made more generic:
private static async Task DeleteDocumentAsync(int partitionKey, string documentID)
{
Console.WriteLine("\n1.7 - Deleting a document");
ResourceResponse<Document> response = await client.DeleteDocumentAsync(
UriFactory.CreateDocumentUri(DatabaseID, CollectionName, documentID),
new RequestOptions { PartitionKey = new PartitionKey(partitionKey) });
Console.WriteLine("Request charge of delete operation: {0}", response.RequestCharge);
Console.WriteLine("StatusCode of operation: {0}", response.StatusCode);
}
The text was updated successfully, but these errors were encountered:
euangordon
changed the title
Code Sample Update
Code Sample Update - Confusion over PartitionKey when deleting
Sep 15, 2021
I have been using Cosmos for some time and recently moved over to use Microsoft.Azure.Documents.Client. I found the documentation pretty easy to get going until I tried to delete documents.
https://github.com/Azure/azure-cosmos-dotnet-v2/blob/master/samples/code-samples/DocumentManagement/Program.cs
I realise that my mistake is somewhat silly, but possibly a couple of comments would have saved me a few hours.
In the sample for deletions:
I wrongly interpretted the PartitionKey to be the PartitionKey set on the container, in my case VesselID, when it should have been the value of the VesselID, in my case an int like 123.
A couple of suggestions to make this more obvious.
In my own code, I ended up with, it could obviously be made more generic:
The text was updated successfully, but these errors were encountered: