using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;
namespace AzureStorageTables
{
class CustomerEntity : TableEntity
{
public CustomerEntity() // required constructor
{
}
public CustomerEntity(string firstName, string lastName)
{
this.PartitionKey = lastName; // more diversity => more scalability
this.RowKey = firstName;
}
public CustomerEntity(string partitionKey, string rowKey, DateTimeOffset timestamp, string etag, IDictionary<string, EntityProperty> props)
{
this.PartitionKey = partitionKey;
this.RowKey = rowKey;
this.Timestamp = timestamp;
this.ETag = etag;
if (props.ContainsKey("EmailAddress")) this.EmailAddress = props["EmailAddress"].StringValue;
if (props.ContainsKey("PhoneNumber")) this.PhoneNumber = props["PhoneNumber"].StringValue;
}
public string EmailAddress { get; set; }
public string PhoneNumber { get; set; }
}
}
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using NFluent;
namespace AzureStorageTables
{
[TestClass]
public class Tests_Tables_02_CreateEntities
{
private static TestContext _Context = null;
private static CloudTableClient _Client = null;
[ClassInitialize]
public static void Class_Init(TestContext ctx)
{
_Context = ctx;
var saName = _Context.Properties["AzureStorageAccountName"] as string;
var saKey = _Context.Properties["AzureStorageAccountAccessKey1"] as string;
var saCreds = new StorageCredentials(saName, saKey);
var saConfig = new CloudStorageAccount(saCreds, true);
_Client = saConfig.CreateCloudTableClient();
}
[TestMethod]
public async Task Test_20_CreateEntity()
{
var table = _Client.GetTableReference("testnewentity");
await table.CreateIfNotExistsAsync();
var e = new CustomerEntity("Anton", "Delsink")
{
EmailAddress = "anton@delsink.com",
PhoneNumber = "+971504532798"
};
var op = TableOperation.Insert(e);
await table.ExecuteAsync(op);
}
[TestMethod]
public async Task Test_21_Batch()
{
var table = _Client.GetTableReference("testnewentity");
await table.CreateIfNotExistsAsync();
var batchOp = new TableBatchOperation();
var customers = LoadCustomers();
foreach (var c in customers)
{
var op = TableOperation.Insert(c);
batchOp.Add(op);
}
await table.ExecuteBatchAsync(batchOp);
}
private List<CustomerEntity> LoadCustomers()
{
var rnd = new Random();
var lst = new List<CustomerEntity>();
for (int ix = 0; ix < 100; ix++) // max batch size
{
string ln = "delsink"; // all batch inserts must have samepartition key
string fn = "fn" + rnd.Next(1000, 5000).ToString(); // likely to generate duplicats!
if (lst.Count<CustomerEntity>((ce) => ce.RowKey == fn) > 0)
continue; // no duplicates!
string pn = rnd.Next(10000000, 99999999).ToString();
string email = fn + "@" + ln + ".com";
lst.Add(new CustomerEntity(fn, ln)
{
EmailAddress = email,
PhoneNumber = pn
});
}
return lst;
}
}
}
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using NFluent;
namespace AzureStorageTables
{
[TestClass]
public class Tests_Tables_30_Queries
{
private static TestContext _Context = null;
private static CloudTableClient _Client = null;
[ClassInitialize]
public static void Class_Init(TestContext ctx)
{
_Context = ctx;
var saName = _Context.Properties["AzureStorageAccountName"] as string;
var saKey = _Context.Properties["AzureStorageAccountAccessKey1"] as string;
var saCreds = new StorageCredentials(saName, saKey);
var saConfig = new CloudStorageAccount(saCreds, true);
_Client = saConfig.CreateCloudTableClient();
}
[TestMethod]
public async Task Test_31_Query()
{
var table = _Client.GetTableReference("testnewentity");
bool tableExists = await table.ExistsAsync();
Check.That(tableExists).IsTrue();
var query = new TableQuery<CustomerEntity>()
.Where(
TableQuery.GenerateFilterCondition(
"PartitionKey",
QueryComparisons.Equal,
"Delsink"));
var customers = new List<CustomerEntity>();
TableContinuationToken tct = null;
do
{
var queryResult = await table.ExecuteQuerySegmentedAsync(query, tct);
tct = queryResult.ContinuationToken;
customers.AddRange(queryResult.Results);
} while (tct != null);
Check.That(customers.Count).IsEqualTo(100);
}
[TestMethod]
public async Task Test_32_Query()
{
var table = _Client.GetTableReference("testnewentity");
bool tableExists = await table.ExistsAsync();
Check.That(tableExists).IsTrue();
var query = new TableQuery<CustomerEntity>()
.Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Delsink"),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "Anton")));
var customers = new List<CustomerEntity>();
TableContinuationToken tct = null;
do
{
var queryResult = await table.ExecuteQuerySegmentedAsync(query, tct);
tct = queryResult.ContinuationToken;
customers.AddRange(queryResult.Results);
} while (tct != null);
Check.That(customers.Count).IsEqualTo(1);
}
[TestMethod]
public async Task Test_33_Query()
{
var table = _Client.GetTableReference("testnewentity");
bool tableExists = await table.ExistsAsync();
Check.That(tableExists).IsTrue();
var query = new TableQuery().Select(new string[] { "EmailAddress" });
var results = new List<DynamicTableEntity>();
TableContinuationToken tct = null;
do
{
var queryResult = await table.ExecuteQuerySegmentedAsync(query, tct);
tct = queryResult.ContinuationToken;
results.AddRange(queryResult.Results);
} while (tct != null);
Check.That(results.Count).IsStrictlyGreaterThan(0);
foreach (DynamicTableEntity e in results)
{
Check.That(e.Properties.TryGetValue("EmailAddress", out EntityProperty v)).IsTrue();
}
}
}
}