Azure Storage CRUD Operations In MVC Using C#
Azure Storage CRUD Operations In MVC Using C# - Azure Table Storage - Part One
This article is about ‘How to execute CRUD Operations in Azure Table Storage.’
In this article, I’m going to describe how to execute CRUD Operations in Azure Table Storage. CRUD stands for Create, Read, Update and Delete.
I’ll show you the entire CRUD operation using ASP.NET MVC application, step-by-step.
Overview
Azure Table is structured column storage on NoSQL Cloud Database. It is not a relational database. Azure Table Storage can store just a table without any foreign keys or any other kind of relation. These tables are highly scalable and ideal for handling a large amount of data. Tables can be stored and queried for a large amount of data. The relational database can be stored using SQL Data Services, which is a separate service.
You can get more overview of Azure Table Storage from here.
To create this project, I’m using,
.NET Framework 4.6
Windows Azure Storage 8.1.1 package
Visual Studio 2015
MVC 5.0
Consideration
To create this CRUD operation, the following skills should be considered.
Good knowledge of ASP.NET MVC 5
OOPs concepts in C#
Basic knowledge of NoSQL
Scope of Work (SOW)
I’m creating a Student database with CRUD operations
Insert and Edit Screen - Here, I used a HTML form. At the time of edit, I’ll fill it with Student data.
Azure
Get and Delete Screen - Here, I use an HTML table. There are three buttons here - Edit, Change Status, and Delete.
Azure
Step 1
Create Azure Storage Account. Go to https://portal.azure.com and create Azure Storage Account.
Azure
Access Key & Connection String
After creating Azure Storage Account, go to that account and copy the access key and connection string of that account. It will be used in our MVC application.
Azure
Step 2
Create a new empty MVC application. Create a new project by going to File -> New -> Project…
Go to NuGet Package Manager console window and install -> “Install-Package WindowsAzure.Storage -Version 8.1.1”.
Step 3
Add “TableManager” class for accessing the Azure Table. And, use two namespaces.
WindowsAzure.Storage;
WindowsAzure.Storage.Table;
Remove the default Constructor from “TableManager” class. And, add a parameterized Constructor with the string parameter.
This constructor will create the table if it’s not there in Storage. We put the Azure Storage Connection String in this constructor, which was declared above.
public class TableManager
{undefined
// private property
private CloudTable table;
// Constructor
public TableManager(string _CloudTableName)
{
if (string.IsNullOrEmpty(_CloudTableName))
{
throw new ArgumentNullException("Table", "Table Name can't be empty");
}
try
{
string ConnectionString = "Your Azure Storage Connection String goes here";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
table = tableClient.GetTableReference(_CloudTableName);
table.CreateIfNotExists();
}
catch (StorageException StorageExceptionObj)
{
throw StorageExceptionObj;
}
catch (Exception ExceptionObj)
{
throw ExceptionObj;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
}
Step 4
Add a generic method for Insert or Merge/Replace into “TableManager” class, where the generic value should be an inherited object of “TableEntity”. I use Repository Pattern here.
Why do I use Generic Method?
Azure Table is Column-based NoSQL. Here, we can store any kind of Data and any Object. This is a flavor of NoSQL. We don’t know, which type of object we are going to insert. If we use a generic method, we can access any object.
public void InsertEntity(T entity, bool forInsert = true) where T : TableEntity, new()
{undefined
try
{undefined
if (forInsert)
{undefined
var insertOperation = TableOperation.Insert(entity);
table.Execute(insertOperation);
}
else
{undefined
var insertOrMergeOperation = TableOperation.InsertOrReplace(entity);
table.Execute(insertOrMergeOperation);
}
}
catch (Exception ExceptionObj)
{undefined
throw ExceptionObj;
}
}
Step 5
Similarly, add another generic method to Retrieve entity into “TableManager” class. In this method, you can pass the query. According to query, it will return the dataset.
Now, the question is, what kind of query can we pass here. Here is the demonstration of how to use the operator in Azure Table Storage.
Operator expression
Equal eq
GreaterThan gt
GreaterThanOrEqual ge
LessThan lt
LessThanOrEqual le
NotEqual ne
And and
Not not
Or or
Example
I’m using Student Object here. I want to get the Student data where the name of the student is equal to Albert Einstein.
The query looks like - Name eq ‘Albert Einstein’
public List RetrieveEntity(string Query = null) where T : TableEntity, new()
{undefined
try
{undefined
// Create the Table Query Object for Azure Table Storage
TableQuery DataTableQuery = new TableQuery();
if (!String.IsNullOrEmpty(Query))
{undefined
DataTableQuery = new TableQuery().Where(Query);
}
IEnumerable IDataList = table.ExecuteQuery(DataTableQuery);
List DataList = new List();
foreach (var singleData in IDataList)
DataList.Add(singleData);
return DataList;
}
catch (Exception ExceptionObj)
{undefined
throw ExceptionObj;
}
}
Step 6
Add another generic method to Delete entity into “TableManager” class.
public bool DeleteEntity(T entity) where T : TableEntity, new()
{undefined
try
{undefined
var DeleteOperation = TableOperation.Delete(entity);
table.Execute(DeleteOperation);
return true;
}
catch (Exception ExceptionObj)
{undefined
throw ExceptionObj;
}
}
Step 7 - Model
Add Model, “Student.cs”, which is inherited from Microsoft.WindowsAzure.Storage.Table.TableEntity.
Define the below properties:
public class Student : TableEntity
{undefined
public Student() { }
public string Name { get; set; }
public string Email { get; set; }
public string Department { get; set; }
public bool? IsActive { get; set; }
- 1
- 2
- 3
- 4
}
Step 8 - Insert and Update
Add a controller named “Home”. There will be a default Index Action Result; just add a parameter named id as string.
Note - I am going to use partition key and row key in the coming codes. If you want to know more about partitionkey and row key, get it from here.
public ActionResult Index(string id)
{undefined
if (!string.IsNullOrEmpty(id))
{undefined
// Get particular student info
TableManager TableManagerObj = new TableManager(“person”); // ‘person’ is the name of the table
// pass query where RowKey eq value of id
List SutdentListObj = TableManagerObj.RetrieveEntity(“RowKey eq '” + id + “’”);
Student StudentObj = SutdentListObj.FirstOrDefault();
return View(StudentObj);
}
return View(new Student());
}
Then, right-click on Index and add an empty View for Insert or Update screen.
@model Student
@using (Html.BeginForm(“Index”, “Home”, new { id = Model.RowKey }, FormMethod.Post))
{undefined
Student Form
}
Put an HttpPost Action Result into Home Controller.
[HttpPost]
public ActionResult Index(string id, FormCollection formData)
{undefined
Student StudentObj = new Student();
StudentObj.Name = formData[“Name”] == “”? null : formData[“Name”];
StudentObj.Department = formData[“Department”] == “”? null: formData[“Department”];
StudentObj.Email = formData[“Email”] == “”? null: formData[“Email”];
// Insert
if (string.IsNullOrEmpty(id))
{
StudentObj.PartitionKey = "Student";
StudentObj.RowKey = Guid.NewGuid().ToString();
TableManager TableManagerObj = new TableManager("person");
TableManagerObj.InsertEntity<Student>(StudentObj, true);
}
// Update
else
{
StudentObj.PartitionKey = "Student";
StudentObj.RowKey = id;
TableManager TableManagerObj = new TableManager("person");
TableManagerObj.InsertEntity<Student>(StudentObj, false);
}
return RedirectToAction("Get");
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
}
Step 9 - Select and Delete
Add another Action Result into the Home Controller, where we can get all records from Azure Storage.
public ActionResult Get()
{undefined
TableManager TableManagerObj = new TableManager(“person”);
List SutdentListObj = TableManagerObj.RetrieveEntity();
return View(SutdentListObj);
}
The View of Get Action Result looks like this.
@model List
@foreach (Student StudentObj in Model) { }
Name | Department | Status | Action | |
---|---|---|---|---|
@StudentObj.Name | @StudentObj.Department | @StudentObj.Email | @if (!StudentObj.IsActive) { | @Html.ActionLink("Edit", "Index", "Home", new { id = StudentObj.RowKey }, new { }) || @Html.ActionLink("Delete", "Delete", "Home", new { id = StudentObj.RowKey }, new { }) |
相关文章