Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
Lab 8: Get started with Azure Table storage and Visual Studio connected services (ASP.NET Core)
Azure Table storage enables you to store large amounts of structured data. The service is a NoSQL datastore that accepts authenticated calls from inside and outside the Azure cloud. Azure tables are ideal for storing structured, non-relational data.
This tutorial shows how to write ASP.NET code for some common scenarios using Azure table storage entities. These scenarios include creating a table, and adding, querying, and deleting table entities.
a.
1. 2.
Create an ASP.NET application project
Estimation time for this section A: 5 Minutes
Open Visual Studio. From the main menu, select File > New > Project.
In the New Project dialog box, select Web > ASP.NET Core Web
Application > StorageAspNet. Then select OK.
3.
In the New ASP.NET Core Web Application dialog box, select .NET
Core > ASP.NET Core 2.0 > Web Application (Model-View-Controller). Then
select OK.
Level 3
Asia Pacific University of Technology & Innovation
Page 1 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
b.
1. 2.
Use connected services to connect to an Azure storage account.
Estimation time for this section B: 10 Minutes In Solution Explorer, right-click the project.
From the context menu, select Add > Connected Service.
In the Connected Services dialog box, select Cloud Storage with Azure Storage, and then select Configure.
3.
Level 3
Asia Pacific University of Technology & Innovation Page 2 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
4.
In the Azure Storage dialog box, select the Azure storage account to be used for this tutorial. To create a new Azure storage account, select Create a New Storage Account, and complete the form.
After selecting either an existing storage account or creating a new one, select Add. Visual Studio installs the NuGet package for Azure Storage, and a storage connection string to appsettings.json.
Level 3
Asia Pacific University of Technology & Innovation Page 3 of 25
5.
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
Level 3 Asia Pacific University of Technology & Innovation Page 4 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
c.
Create a controller.
Estimation time for this section C: 10 Minutes
1. In Solution Explorer, create a Controllers folder. Then, right-click Controllers.
2. From the context menu, select Add > Controller.
Level 3
Asia Pacific University of Technology & Innovation
Page 5 of 25
3. In the Add Scaffold dialog box, select MVC Controller – select Add.
Empty, and
4. In the Add Empty MVC Controller dialog box, name the controller TablesController, and select Add.
5. Add the following using directives to the TablesController.cs file:
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Auth;
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
d. Createamodelclass.
Estimation time for this section D: 5 Minutes
Many of the examples in this article use aTableEntity-derived class called CustomerEntity. The following steps guide you through declaring this class as a model class.
1. In the Solution Explorer, right-click Models, and, from the context menu, select Add > Class.
2. On the Add New Item dialog, name the class, CustomerEntity.
3. Open the CustomerEntity.cs file, and add the following using directive: using Microsoft.WindowsAzure.Storage.Table;
4. Modify the class so that, when finished, the class is declared as in the following code. The class declares an entity class called CustomerEntity that uses the customer’s first name as the row key and last name as the partition key.
Level 3
Asia Pacific University of Technology & Innovation Page 6 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
e. Create a table.
Estimation time for this section E: 10 Minutes The following steps illustrate how to create a table.
1. Open the TablesController.cs file.
2. Add a method called CreateTable that an ActionResult.
3. In Solution Explorer, right-click the Views folder. From the context menu, select Add > New Folder. Name the new folder Tables.
4. In Solution Explorer, expand the Views folder, and right-click Tables. From the context menu, select Add > View.
Level 3
Asia Pacific University of Technology & Innovation Page 7 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
5. In the Add MVC View box, enter CreateTable for the view name, and select Add.
6. Open CreateTable.cshtml, and modify it so that it looks like the following code snippet:
7. In Solution Explorer, expand the Views > Shared folder, and
open _Layout.cshtml. Look for the unordered list that looks like this:
. After the last
- element in the list, add the following HTML to add another navigation menu item:
-
Create Azure Table Storage
Level 3
Asia Pacific University of Technology & Innovation
Page 8 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
8. Run the application, and select Create Azure Table Storage to see results similar to the following screenshot:
Level 3
Asia Pacific University of Technology & Innovation Page 9 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
f. Add an entity to a table.
Estimation time for this section F: 15 Minutes
Entities map to C# objects by using a custom class derived from TableEntity. To add an entity
to a table, create a class that defines the properties of your entity.
In this section, you’ll see how to define an entity class that uses the customer’s first name as the row key and last name as the partition key. Together, an entity’s partition and row key uniquely identify the entity in the table.
Entities with the same partition key can be queried faster than entities with different partition keys, but using diverse partition keys allows for greater scalability of parallel operations.
For any property that should be stored in the table service, the property must be a public property of a supported type that exposes both setting and retrieving values. The entity class must declare a public parameter-less constructor.
The steps below use to add an entity to a table:
1. Open the TablesController.cs file.
2. Add the following directive so that the code in the TablesController.cs file can access the CustomerEntity class:
using StorageAspNet.Models;
3. Add a method called AddEntity that returns an ActionResult.
4. Within the AddEntity method, get a CloudStorageAccount object that represents your storage account information. Use the following code to get the storage connection string and storage account information from the Azure service configuration: (Change to the name of the Azure storage account you’re accessing.) Get a CloudTable object that represents a reference to the table to which you are going to add the new entity.
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”);
IConfigurationRoot Configuration = builder.Build();
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(Configuration[“ConnectionStrings:AzureStorageC
onnectionString-1”]);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(“TestTable”);
Level 3
Asia Pacific University of Technology & Innovation Page 10 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
5. Instantiate and initialize the CustomerEntity class.
6. Create a TableOperation object that inserts the customer entity. TableOperation insertOperation = TableOperation.Insert(customer1);
7. Execute the insert operation by calling the CloudTable.Execute method. You can verify the result of the operation by inspecting the TableResult.HttpStatusCode property.
TableResult result = table.ExecuteAsync(insertOperation).Result;
8. Update the ViewBag with the table name, and the results of the insert operation.
9. In Solution Explorer, expand the Views folder, and right-click Tables. From the context menu, select Add > View. In the Add MVC View box, enter AddEntity for the view name, and select Add.
9. Open AddEntity.cshtml, and modify it so that it looks like the following code snippet:
10. In Solution Explorer, expand the Views > Shared folder, and
open _Layout.cshtml. After the last
- element in the list, add the following HTML to add another navigation menu item:
CustomerEntity customer1 = new CustomerEntity(“Harp”, “Walter”);
customer1.Email = “Walter@contoso.com”;
ViewBag.TableName = table.Name;
ViewBag.Result = result.HttpStatusCode;
Entity
Add
Level 3
Asia Pacific University of Technology & Innovation Page 11 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
11. Run the application, and select Add Entity to see results similar to the following screenshot.
12. To view all the entities for your tables, you can use the Microsoft Azure Storage Explorer.
Level 3
Asia Pacific University of Technology & Innovation Page 12 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
g. Add a batch of entities to a table.
Estimation time for this section G: 10 minutes.
In addition to being able to add an entity to a table one at a time, you can also add
entities in batch.
Adding entities in batch reduces the number of round-trips between your code and the Azure table service.
The following steps illustrate how to add multiple entities to a table with a single insert operation.
1. Open the TablesController.cs file.
2. Add a method called AddEntities that returns an ActionResult.
3. Within the AddEntities method, add the below connection code to the Azure Table Storage:
var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(“appsettings.json”); IConfigurationRoot Configuration = builder.Build();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Configuration[“ConnectionStrings:AzureStorageConnection
String-1”]);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference(“TestTable”);
4. Then, instantiate some customer objects based on the CustomerEntity model class presented in the section of “Add an entity to a table”.
5. Get a TableBatchOperation object.
TableBatchOperation batchOperation = new TableBatchOperation();
CustomerEntity customer1 = new CustomerEntity(“Smith”, “Jeff”); customer1.Email = “Jeff@contoso.com”;
CustomerEntity customer2 = new CustomerEntity(“Smith”, “Ben”); customer2.Email = “Ben@contoso.com”;
Level 3
Asia Pacific University of Technology & Innovation Page 13 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
6. Add entities to the batch insert operation object.
7. Execute the batch insert operation by calling the CloudTable.ExecuteBatchAsync method.
IList results = table.ExecuteBatchAsync(batchOperation).Result;
8. The CloudTable.ExecuteBatch method returns a list of TableResult objects where each TableResult object can be examined to determine the success or failure of each individual operation. For this example, pass the list to a view and let the view display the results of each operation.
return View(results);
9. The final coding screenshot for the AddEntities() method as below:
10. In Solution Explorer, expand the Views folder, and right-click Tables. From the context menu, select Add > View. In the Add MVC View box,
enter AddEntities for the view name, and select Add.
11. Open AddEntities.cshtml, and replace the contents with the following code:
Level 3
Asia Pacific University of Technology & Innovation Page 14 of 25
batchOperation.Insert(customer1); batchOperation.Insert(customer2);
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
12. In Solution Explorer, expand the Views > Shared folder, and
open _Layout.cshtml. After the last
- element in the list, add the following HTML to add another navigation menu item:
13. Run the application, and select Add Multiple Entities to see results similar to the following screenshot:
Multiple Entities
Add
Level 3
Asia Pacific University of Technology & Innovation Page 15 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
h. Get a Single Entity
Estimation time for this section H: 10 Minutes.
This section illustrates how to get a single entity from a table using the entity’s row key
and partition key.
1. Open the TablesController.cs file.
2. Add a method called GetSingle that returns an ActionResult.
3. Within the GetSingle method, add the below connection code to the Azure Table Storage:
var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(“appsettings.json”); IConfigurationRoot Configuration = builder.Build();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Configuration[“ConnectionStrings:AzureStorageConnection String-1”]);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference(“TestTable”);
4. Create a retrieve operation object that takes an entity object derived from TableEntity. The first parameter is the partitionKey, and the second parameter is the rowKey. Using the CustomerEntity class and data presented in the section Add a batch of entities to a table, the following code snippet queries the table for a CustomerEntity entity with a partitionKey value of “Smith” and a rowKey value of “Ben”:
5. Execute the retrieve operation.
TableResult result = table.ExecuteAsync(retrieveOperation).Result;
6. Pass the result to the view for display.
return View(result);
TableOperation retrieveOperation = TableOperation.Retrieve(“Smith”, “Ben”);
Level 3
Asia Pacific University of Technology & Innovation
Page 16 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
7. The final coding screenshot for the GetSingle() method as below:
8. In Solution Explorer, expand the Views folder, and right-click Tables. From the context menu, select Add > View. In the Add MVC View box, enter GetSingle for the view name, and select Add.
9. Open GetSingle.cshtml, and replace the contents with the following code:
10. In Solution Explorer, expand the Views > Shared folder, and
open _Layout.cshtml. After the last
- element in the list, add the following HTML to add another navigation menu item:
11. Run the application, and select Get Single Entity to see results similar to the following screenshot:
Single Entity
Get
Level 3
Asia Pacific University of Technology & Innovation Page 17 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
i. Get all entities in a partition.
Estimation time for this section I: 10 Minutes.
As mentioned in the section, Add an entity to a table, the combination of a partition and a row key uniquely identify an entity in a table. Entities with the same partition key can be queried faster than entities with different partition keys. This section illustrates how to query a table for all the entities from a specified partition.
1. Open the TablesController.cs file.
2. Add a method called GetPartition that returns an ActionResult.
3. Within the GetSingle method, add the below connection code to the Azure Table Storage:
4. Instantiate a TableQuery object specifying the query in the Where clause. Using the CustomerEntity class and data presented in the section Add a batch of entities to a table, the following code snippet queries the table for all entities where the PartitionKey (customer’s last name) has a value of “Smith”:
5. Within a loop, call the CloudTable.ExecuteQuerySegmented method passing the query object you instantiated in the previous step.
The CloudTable.ExecuteQuerySegmented method returns
a TableContinuationToken object that – when null – indicates that there are no more entities to retrieve. Within the loop, use another loop to iterate over the returned entities. In the following code example, each returned entity is added to a list. Once the loop ends, the list is passed to a view for display.
Level 3
Asia Pacific University of Technology & Innovation Page 18 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
6. The final coding screenshot for the GetPartition() method as below:
7. In Solution Explorer, expand the Views folder, and right-click Tables. From the context menu, select Add > View. In the Add MVC View box, enter GetPartition for the view name, and select Add.
8. Open GetPartition.cshtml, and replace the contents with the following code:
Level 3
Asia Pacific University of Technology & Innovation Page 19 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
9. In Solution Explorer, expand the Views > Shared folder, and
open _Layout.cshtml. After the last
- element in the list, add the following HTML to add another navigation menu item:
10. Run the application, and select Get Multiple Entities to see results similar to the following screenshot:
Get
Level 3
Asia Pacific University of Technology & Innovation Page 20 of 25
Multiple Entities
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
j.
Delete an entity.
Estimation time for this section J: 10 Minutes.
This section illustrates how to delete an entity from a table.
1.
Open the GetPartition.cshtml file and modify the code. Add a button for
each entity that display in the table.
Then, open the TablesController.cs file. Add a method called DeleteEntity in that file. The DeleteEntity will returns an ActionResult. The code that need to add in the method was shown as below.
2.
delete
Level 3
Asia Pacific University of Technology & Innovation Page 21 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
Level 3
Asia Pacific University of Technology & Innovation Page 22 of 25
3.
Now, go the GetPartition() method in TablesController.cs file. Modify the code sentence of public ActionResult GetPartition() to become public
ActionResult GetPartition(int id = 0)
4.
Open GetPartition.cshtml, and replace the contents with the following code:
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
5. Run the application, and select Get Multiple Entities to see results similar to the following screenshot:
6. Then, click on one of the Delete button, you will see the results similar to the following screenshot. This means, the data have been deleted from the action.
7. Exercise 01: For now, it just able to show the “Smith” results in the “Get Partition Results” page. Try to modify the GetPartition() method in TablesController.cs file, so that all the entities from the table storage will be displaying in the page.
Level 3
Asia Pacific University of Technology & Innovation Page 23 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
8. Exercise 02: Try to publish this application to the Azure Cloud with Azure Web Application Service
Level 3 Asia Pacific University of Technology & Innovation Page 24 of 25
Design and Developing Application in Cloud (CT071-3-5-3-DDAC) Azure Table Storage and Visual Studio Connected Services
k. Home Exercise: Combine the Lab 7 Exercise 03 system with the Table Storage
Estimation time for this section K: 30 Minutes.
1. Open the Folder Explorer and go to C:\Users\\source\repos. Inside the repos folder, try to open the EFGetStarted.AspNetCore.NewDb (lab 7 exercise 03 system) project solution. Follow the steps in lab 7, try to add on the customer information for the blog system by using the Azure Table Storage.
2. Once you finish all the tutorials and exercises in this Lab 8, remember to delete all the created resources from your Azure account.
Summary:
In this tutorial, we learned how to build a system with the Azure Table Storage Service. We also learned how to combine the existing system (with SQL database + Azure Blob Storage) with the Azure Table Storage Service.
Level 3 Asia Pacific University of Technology & Innovation Page 25 of 25