Adding items to the container

In this part, we are going to add items to the container using the API. First, we need to create a Family class that represents the objects that we are going to store in the container. You can create an item using the CreateItemAsync method. When you use the SQL API, items are created and stored as documents. These documents are user-defined arbitrary JSON content. You can then insert an item into your Azure Cosmos container.

Besides the Family class, we will also add some subclasses, such as Parent, Child, Pet, and Address, which are used in the Family class. Therefore, we have to take the following steps:

  1. Add a new class to the project and call it Family.cs:

Creating a new class
  1. Click Add.
  2. Replace the references with the following (you should be prompted to install the NuGet package for this; if not, open the NuGet package manager and add it manually):
using Newtonsoft.Json;
  1. Then add the following code to create the Family class:
public class Family
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public string LastName { get; set; }
public Parent[] Parents { get; set; }
public Child[] Children { get; set; }
public Address Address { get; set; }
public bool IsRegistered { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
  1. Then we can create the Child class below the Family class:
 public class Parent
{
public string FamilyName { get; set; }
public string FirstName { get; set; }
}
  1. Next, we will create the Pet class below the Child class:
public class Child
{
public string FamilyName { get; set; }
public string FirstName { get; set; }
public string Gender { get; set; }
public int Grade { get; set; }
public Pet[] Pets { get; set; }
}
  1. Lastly, we will create the Address class below the Pet class:
 public class Address
{
public string State { get; set; }
public string County { get; set; }
public string City { get; set; }
}
  1. In Program.cs, add the AddItemsToContainerAsync method after your CreateContainerAsync method and create the first Family item:
 private async Task AddItemsToContainerAsync()
{
Family ZaalFamily = new Family
{
Id = "Zaal.1",
LastName = "Zaal",
Parents = new Parent[]
{
new Parent { FirstName = "Thomas" },
new Parent { FirstName = "Sjoukje" }
},
Children = new Child[]
{
new Child
{
FirstName = "Molly",
Gender = "female",
Grade = 5,
Pets = new Pet[]
{
new Pet { GivenName = "Fluffy" }
}
}
},
Address = new Address { State = "WA", County = "King", City = "Seattle" },
IsRegistered = false
};
  1. Then, below item, add the code to add item to container:
     try
{
// Read the item to see if it exists.
ItemResponse<Family> zaalFamilyResponse = await this.container.ReadItemAsync<Family>(ZaalFamily.Id, new PartitionKey(ZaalFamily.LastName));
Console.WriteLine("Item in database with id: {0} already exists ", zaalFamilyResponse.Resource.Id);
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// Create an item in the container representing the Zaal family. Note we provide the value of the partition key for this item, which is "Zaal"
ItemResponse<Family> zaalFamilyResponse = await this.container.CreateItemAsync<Family>(ZaalFamily, new PartitionKey(ZaalFamily.LastName));

// 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.
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs. ", zaalFamilyResponse.Resource.Id, zaalFamilyResponse.RequestCharge);
}
  1. For the second Family item, right underneath the previous code (in the same method), add the following: 
 // Create a family object for the PacktPub family
Family PacktPubFamily = new Family
{
Id = "PacktPub.1",
LastName = "PacktPub",
Parents = new Parent[]
{
new Parent { FamilyName = "PacktPub", FirstName = "Robin" },
new Parent { FamilyName = "Zaal", FirstName = "Sjoukje" }
},
  1. Add children to Family:
   Children = new Child[]
{
new Child
{
FamilyName = "Merriam",
FirstName = "Jesse",
Gender = "female",
Grade = 8,
Pets = new Pet[]
{
new Pet { GivenName = "Goofy" },
new Pet { GivenName = "Shadow" }
}
},
new Child
{
FamilyName = "Miller",
FirstName = "Lisa",
Gender = "female",
Grade = 1
}
},
Address = new Address { State = "NY", County = "Manhattan", City = "NY" },
IsRegistered = true
};
  1. Then, below item, add the code to add item to container again:
  try
{
// Read the item to see if it exists
ItemResponse<Family> packtPubFamilyResponse = await this.container.ReadItemAsync<Family> (PacktPubFamily.Id, new PartitionKey(PacktPubFamily.LastName));
Console.WriteLine("Item in database with id: {0} already exists ", packtPubFamilyResponse.Resource.Id);
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
// Create an item in the container representing the Wakefield family. Note we provide the value of the partition key for this item, which is "PacktPub"
ItemResponse<Family> packtPubFamilyResponse = await this.container.CreateItemAsync<Family>(PacktPubFamily, new PartitionKey(PacktPubFamily.LastName));

// 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.
Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs. ", packtPubFamilyResponse.Resource.Id, packtPubFamilyResponse.RequestCharge);
}
}
  1. Finally, we need to call AddItemsToContainerAsync in the GetStartedDemoAsync method again:
  public async Task GetStartedDemoAsync()
{
// Create a new instance of the Cosmos Client
this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey);
await this.CreateDatabaseAsync();
await this.CreateContainerAsync();

//ADD THIS PART TO YOUR CODE
await this.AddItemsToContainerAsync();
}
  1. Now, run the application again. The items will be added to the container.

In this part, we have added some items to the container. In the next part, we will query the resources.

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

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