Создание модели данных для элементов в Table Storage
9. public class GuestBookEntry: 10. Microsoft.WindowsAzure.StorageClient.TableServiceEntity 11. { }
13.public GuestBookEntry() 14. { 15. PartitionKey = DateTime.UtcNow.ToString("MMddyyyy"); 16. 17. // Row key allows sorting, so we make sure the rows come back in time order. 18. RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid()); }
20.public string Message { get; set; } 21. public string GuestName { get; set; } 22. public string PhotoUrl { get; set; } public string ThumbnailUrl { get; set; }
26.using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient;
28.public class GuestBookDataContext: TableServiceContext 29. { 30. public GuestBookDataContext(string baseAddress, StorageCredentials credentials) 31.: base(baseAddress, credentials) 32. { } }
34.public class GuestBookDataContext: TableServiceContext 35. { 36.... 37. public IQueryable<GuestBookEntry> GuestBookEntry 38. { 39. get 40. { 41. return this.CreateQuery<GuestBookEntry>("GuestBookEntry"); 42. } 43. } }
46.using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient;
48.public class GuestBookEntryDataSource 49. { 50. private static CloudStorageAccount storageAccount; 51. private GuestBookDataContext context; }
53.public class GuestBookEntryDataSource 54. { 55. private static CloudStorageAccount storageAccount; 56. private GuestBookDataContext context; 57. 58. static GuestBookEntryDataSource() 59. { 60. storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); 61. 62. CloudTableClient.CreateTablesFromModel( 63. typeof(GuestBookDataContext), 64. storageAccount.TableEndpoint.AbsoluteUri, 65. storageAccount.Credentials); 66. } }
68.public GuestBookEntryDataSource() 69. { 70. this.context = new GuestBookDataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials); 71. this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1)); }
73.public IEnumerable<GuestBookEntry> Select() 74. { 75. var results = from g in this.context.GuestBookEntry 76. where g.PartitionKey == DateTime.UtcNow.ToString("MMddyyyy") 77. select g; 78. return results; 79. } 80. 81. public void UpdateImageThumbnail(string partitionKey, string rowKey, string thumbUrl) 82. { 83. var results = from g in this.context.GuestBookEntry 84. where g.PartitionKey == partitionKey && g.RowKey == rowKey 85. select g; 86. 87. var entry = results.FirstOrDefault<GuestBookEntry>(); 88. entry.ThumbnailUrl = thumbUrl; 89. this.context.UpdateObject(entry); 90. this.context.SaveChanges(); }
|