Windows Azure Storage Simplified
While working with Windows Azure it makes me feel as if I am working on ADO.NET Entity Framework and WCF Data Service. Everything is entity based and connection opens up through context.
Lets see how can we create an application to add Movies to our database.
Following are the using block entry
We need Movies entity
After that you need to choose the data source. In this case we are using development fabric. We can just change the values of the fabric to actual azure with 512 bit key and account name.
Now we need to initialize the RoleInstance to be able to read the config values
Hence after that initialize the CloudStorageAccount and CloudTableClient.
You may use the LINQ to read also with a combination of AsTableServiceQuery
That’s it!!!
The complete code
//Initializing Role of cloud project to be able to read the config
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetting) =>
{
configSetting(RoleEnvironment.GetConfigurationSettingValue(configName));
});
//Reading the connection from Config
CloudStorageAccount sAcc;
sAcc = CloudStorageAccount.FromConfigurationSetting("DataConnection");
//Initializing the CloudTableClient
CloudTableClient tableClient = new CloudTableClient(sAcc.TableEndpoint.AbsoluteUri, sAcc.Credentials);
//Setting the table name
string tableName = "Movies";
//Creating the table in storage
tableClient.CreateTableIfNotExist(tableName);
//Initializung Context
TableServiceContext context = tableClient.GetDataServiceContext();
//Adding new entry
context.AddObject(tableName,
new Movies()
{
PartitionKey = "Action",
RowKey = Guid.NewGuid().ToString(),
Timestamp = DateTime.Now,
Title = "Never Released..."
});
//Saving with retries
context.SaveChangesWithRetries();
//Reading the values using LINQ
var q = (from m in context.CreateQuery<Movies>(tableName)
where m.PartitionKey == "Action"
select m).AsTableServiceQuery<Movies>();
dg.DataSource = q.ToList();
dg.DataBind();
Namoskar!!!