.NET Core CRUD Operation Using Azure Table API

In this article, you will learn about .NET Core CRUD operation using Azure Table API.

Introduction

 
Azure Table is a highly available NoSQL service, to help us build a scalable application. This makes our development process much easier. It allows us to change our model inside our application without mentioning the table schemas.
  • Key-Value pair
  • Low Latency
  • Scalable Apps and High availability
  • Flexible Data Structure
  • JSON to serialize the data
  • The primary region will have read/write but the secondary region will be read-only
.NET Core CRUD Opertaion Using Azure Table API
  • Create a .NET Core application and install NuGet Packages “Microsoft.Azure.Cosmos.Table”. Then, go here and navigate to storage account and get the connection key under “Access Keys” tab.
  • Create an object for “CloudTableClient”, which allows us to perform actions against the table inside the storage accounts.
  • Create a custom table entity and inherit “TableEntity” class, it comes with a lot of important items like partition key, row key, etc.
  • Write a repository method to populate all the values under one particular table, for this demo we are using a synchronous approach, but in real-time we should be async and await approach for a better experience.
  • “TableQuery” will allow us to perform specific actions like insert, delete, update on a particular table.
  • Create a controller class and methods to pull the records from Azure Tables. Create models under the “Model” folder that represent data that we want to display in the UI. The partition key is used for grouping particular rows in the tables.
  • Variety of filters are available in azure table API, that can be performed on table query i.e. generate filter condition for binary, bool, date and many more.
  • Create a “TableOperation” to perform insert, delete and update on specific tables like below.
  • Create a controller and view pages to perform appropriate actions on the UI.
  • In this demo, we have used MVC scaffolding technique to generate the UI
  • Follow the simple above steps in order to get the details from the Azure Table API and kindly, let me know if you are facing any issues.
Repository.CS
  1. public class MyDemoRepository    
  2.     {    
  3.         private CloudTable mytable = null;    
  4.         public MyDemoRepository()    
  5.         {    
  6.             var storageAccount = CloudStorageAccount.Parse("your azure storage key goes here..");    
  7.             var cloudTableClient = storageAccount.CreateCloudTableClient();    
  8.             mytable = cloudTableClient.GetTableReference("Customer");    
  9.             mytable.CreateIfNotExists();    
  10.         }    
  11.     
  12.         public IEnumerable<MyTableEntity> GetAll()    
  13.         {    
  14.             var query = new TableQuery<MyTableEntity>();    
  15.             var entties = mytable.ExecuteQuery(query);    
  16.             return entties;    
  17.         }    
  18.         public void CreateOrUpdate(MyTableEntity myTableOperation)    
  19.         {    
  20.             var operation = TableOperation.InsertOrReplace(myTableOperation);    
  21.             mytable.Execute(operation);    
  22.         }    
  23.         public void Delete(MyTableEntity myTableOperation)    
  24.         {    
  25.             var operation = TableOperation.Delete(myTableOperation);    
  26.             mytable.Execute(operation);    
  27.         }    
  28.         public MyTableEntity Get(string partitionKey, string RowId)    
  29.         {    
  30.             var operation = TableOperation.Retrieve<MyTableEntity>(partitionKey, RowId);    
  31.             var result= mytable.Execute(operation);    
  32.             return result.Result as MyTableEntity;    
  33.         }    
  34.     }    
  35.     
  36.     public class MyTableEntity : TableEntity    
  37.     {    
  38.         public string Name { getset; }    
  39.         public string Address { getset; }    
  40.     }  
.NET Core CRUD Opertaion Using Azure Table API
 
.NET Core CRUD Opertaion Using Azure Table API 
 
CustomerController.cs
  1. public class CustomerController : Controller    
  2.     {    
  3.         //Get the data from Azure table    
  4.         public IActionResult GetAll()    
  5.         {    
  6.             var repositoty = new MyDemoRepository();    
  7.             var entities = repositoty.GetAll();    
  8.             var model = entities.Select(x => new CustomerModel    
  9.             {    
  10.                 Group = x.PartitionKey,    
  11.                 ID = x.RowKey,    
  12.                 Name = x.Name,    
  13.                 Addres = x.Address    
  14.             });    
  15.             return View(model);    
  16.         }    
  17.         public IActionResult Create()    
  18.         {    
  19.             return View();    
  20.         }    
  21.         public IActionResult Edit(string group, string id)    
  22.         {    
  23.             var repositoty = new MyDemoRepository();    
  24.             var item = repositoty.Get(group, id);    
  25.             return View("Edit"new CustomerModel    
  26.             {    
  27.                 Group = item.PartitionKey,    
  28.                 ID = item.RowKey,    
  29.                 Name = item.Name,    
  30.                 Addres = item.Address    
  31.             });    
  32.                
  33.         }    
  34.         public IActionResult ConfirmDelete(string group, string id)    
  35.         {    
  36.             var repositoty = new MyDemoRepository();    
  37.             var item = repositoty.Get(group, id);    
  38.             return View("Delete"new CustomerModel    
  39.             {    
  40.                 Group = item.PartitionKey,    
  41.                 ID = item.RowKey,    
  42.                 Name = item.Name,    
  43.                 Addres = item.Address    
  44.             });    
  45.     
  46.         }    
  47.     
  48.         [HttpPost]    
  49.         public IActionResult Delete(string group, string id)    
  50.         {    
  51.             var repositoty = new MyDemoRepository();    
  52.             var item = repositoty.Get(group, id);    
  53.             repositoty.Delete(item);    
  54.             return RedirectToAction("GetAll");    
  55.     
  56.         }    
  57.         [HttpPost]    
  58.         public IActionResult Create(CustomerModel customerModel)    
  59.         {    
  60.             var repositoty = new MyDemoRepository();    
  61.             repositoty.CreateOrUpdate(new MyTableEntity    
  62.             {    
  63.                 PartitionKey = customerModel.Group,    
  64.                 RowKey = Guid.NewGuid().ToString(),    
  65.                 Name = customerModel.Name,    
  66.                 Address = customerModel.Addres    
  67.     
  68.             });    
  69.             return RedirectToAction("GetAll");    
  70.         }    
  71.     
  72.         [HttpPost]    
  73.         public IActionResult Edit(CustomerModel customerModel)    
  74.         {    
  75.             var repositoty = new MyDemoRepository();    
  76.             repositoty.CreateOrUpdate(new MyTableEntity    
  77.             {                   
  78.                 Name = customerModel.Name,    
  79.                 Address = customerModel.Addres    
  80.     
  81.             });    
  82.             return RedirectToAction("GetAll");    
  83.         }    
  84.     }    
.NET Core CRUD Opertaion Using Azure Table API
 
.NET Core CRUD Opertaion Using Azure Table API
 
.NET Core CRUD Opertaion Using Azure Table API
 
.NET Core CRUD Opertaion Using Azure Table API
 

Summary

 
In this section, we have created a simple CRUD application in .NET Core and retrieved data from Azure Table API Service.