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.

Repository.cs

C#
public class MyDemoRepository    
{    
    private CloudTable mytable = null;    
    public MyDemoRepository()    
    {    
        var storageAccount = CloudStorageAccount.Parse("your azure storage key goes here..");    
        var cloudTableClient = storageAccount.CreateCloudTableClient();    
        mytable = cloudTableClient.GetTableReference("Customer");    
        mytable.CreateIfNotExists();    
    }    
    
    public IEnumerable<MyTableEntity> GetAll()    
    {    
        var query = new TableQuery<MyTableEntity>();    
        var entties = mytable.ExecuteQuery(query);    
        return entties;    
    }    
    public void CreateOrUpdate(MyTableEntity myTableOperation)    
    {    
        var operation = TableOperation.InsertOrReplace(myTableOperation);    
        mytable.Execute(operation);    
    }    
    public void Delete(MyTableEntity myTableOperation)    
    {    
        var operation = TableOperation.Delete(myTableOperation);    
        mytable.Execute(operation);    
    }    
    public MyTableEntity Get(string partitionKey, string RowId)    
    {    
        var operation = TableOperation.Retrieve<MyTableEntity>(partitionKey, RowId);    
        var result= mytable.Execute(operation);    
        return result.Result as MyTableEntity;    
    }    
}    
    
public class MyTableEntity : TableEntity    
{    
    public string Name { get; set; }    
    public string Address { get; set; }    
}

Output 2-

Customer controller 3-

CustomerController.cs

C#
public class CustomerController : Controller
{
    //Get the data from Azure table
    public IActionResult GetAll()
    {
        var repositoty = new MyDemoRepository();
        var entities = repositoty.GetAll();
        var model = entities.Select(x => new CustomerModel
        {
            Group = x.PartitionKey,
            ID = x.RowKey,
            Name = x.Name,
            Addres = x.Address
        });
        return View(model);
    }
    public IActionResult Create()
    {
        return View();
    }
    public IActionResult Edit(string group, string id)
    {
        var repositoty = new MyDemoRepository();
        var item = repositoty.Get(group, id);
        return View("Edit", new CustomerModel
        {
            Group = item.PartitionKey,
            ID = item.RowKey,
            Name = item.Name,
            Addres = item.Address
        });
    }
    public IActionResult ConfirmDelete(string group, string id)
    {
        var repositoty = new MyDemoRepository();
        var item = repositoty.Get(group, id);
        return View("Delete", new CustomerModel
        {
            Group = item.PartitionKey,
            ID = item.RowKey,
            Name = item.Name,
            Addres = item.Address
        });

    }

    [HttpPost]
    public IActionResult Delete(string group, string id)
    {
        var repositoty = new MyDemoRepository();
        var item = repositoty.Get(group, id);
        repositoty.Delete(item);
        return RedirectToAction("GetAll");

    }
    [HttpPost]
    public IActionResult Create(CustomerModel customerModel)
    {
        var repositoty = new MyDemoRepository();
        repositoty.CreateOrUpdate(new MyTableEntity
        {
            PartitionKey = customerModel.Group,
            RowKey = Guid.NewGuid().ToString(),
            Name = customerModel.Name,
            Address = customerModel.Addres

        });
        return RedirectToAction("GetAll");
    }

    [HttpPost]
    public IActionResult Edit(CustomerModel customerModel)
    {
        var repositoty = new MyDemoRepository();
        repositoty.CreateOrUpdate(new MyTableEntity
        {
            Name = customerModel.Name,
            Address = customerModel.Addres

        });
        return RedirectToAction("GetAll");
    }
}

Add MVC view 4-

Create 5-

Delete 6-

Edit 7-

Summary

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