Deleting an item

To delete a Family item, take the following steps:

  1. Add the DeleteFamilyItemAsync method after your ReplaceFamilyItemAsync method:
 private async Task DeleteFamilyItemAsync()
{
var partitionKeyValue = "Zaal";
var familyId = "Zaal.1";

// Delete an item. Note we must provide the partition key value and id of the item to delete
_ = await this.container.DeleteItemAsync<Family>(familyId, new PartitionKey(partitionKeyValue));
Console.WriteLine("Deleted Family [{0},{1}] ", partitionKeyValue, familyId);
}
  1. Then, add a call to DeleteFamilyItemAsync in the GetStartedDemoAsync method:
 public async Task GetStartedDemoAsync()
{
// Create a new instance of the Cosmos Client
this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey);
await this.CreateDatabaseAsync();
await this.CreateContainerAsync();
await this.AddItemsToContainerAsync();
await this.QueryItemsAsync();
await this.ReplaceFamilyItemAsync();

//ADD THIS PART TO YOUR CODE
await this.DeleteFamilyItemAsync();
}
  1. Run the application, and you will see that the item is deleted.

In this demo, we created a Cosmos DB server, database, and container. We also added data to the container, ran a query on the data, updated the data, and lastly, we deleted the data.

In the next section, we are going to cover partitioning schemes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.204.201