Last active
December 15, 2016 08:44
-
-
Save ntotten/4718699 to your computer and use it in GitHub Desktop.
Windows Azure Table Storage Tip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); | |
CloudTableClient.CreateTablesFromModel( | |
typeof(TableServiceContext), | |
account.TableEndpoint.AbsoluteUri, | |
account.Credentials); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerEntity : TableServiceEntity { | |
public CustomerEntity() | |
: base("_default", null) { | |
} | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
public class ProductEntity : TableServiceEntity { | |
public ProductEntity() | |
: base("_default", null) { | |
} | |
public string Name { get; set; } | |
public int Price { get; set; } | |
} | |
public class OrderEntity : TableServiceEntity { | |
public OrderEntity() | |
: base("_default", null) { | |
} | |
public string ProductRowkey { get; set; } | |
public string CustomerRowKey { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); | |
var context = new TableStorageContext(account.TableEndpoint.ToString(), account.Credentials); | |
var customer = context.Customers.SingleOrDefault(c => c.RowKey == "1234"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TableStorageContext : TableServiceContext { | |
private const string CustomersTable = "Customers"; | |
private const string ProductsTable = "Products"; | |
private const string OrdersTable = "Orders"; | |
public TableStorageContext(string baseAddress, StorageCredentials credentials) | |
: base(baseAddress, credentials) { | |
} | |
public IQueryable<CustomerEntity> Customers { | |
get { | |
return this.CreateQuery(CustomersTable).Where(c => c.PartitionKey == "_default"); | |
} | |
} | |
public IQueryable<ProductEntity> Products { | |
get { | |
return this.CreateQuery(ProductsTable).Where(c => c.PartitionKey == "_default"); | |
} | |
} | |
public IQueryable<OrderEntity> Orders { | |
get { | |
return this.CreateQuery(OrdersTable).Where(c => c.PartitionKey == "_default"); | |
} | |
} | |
public void AddCustomer(CustomerEntity customer) { | |
this.AddObject(CustomersTable, customer); | |
} | |
public void AddProduct(ProductEntity product) { | |
this.AddObject(ProductsTable, product); | |
} | |
public void AddOrder(OrderEntity order) { | |
this.AddObject(OrdersTable, order); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment