Context 类
- public class FileContext : TableServiceContext
- {
- public FileContext(string baseAddress, StorageCredentials credentials)
- : base(baseAddress, credentials)
- {
- }
- /// <summary>
- /// Get all entities of table storage "files".
- /// </summary>
- public IEnumerable<FileEntity> GetEntities
- {
- get
- {
- var list = this.CreateQuery<FileEntity>("files");
- return list;
- }
- }
- }
DataSource 类
- public class FileDataSource
- {
- private static CloudStorageAccount account;
- private FileContext context;
- public FileDataSource()
- {
- // Create table storage client via cloud account.
- account = CloudStorageAccount.FromConfigurationSetting("StorageConnections");
- CloudTableClient client = account.CreateCloudTableClient();
- client.CreateTableIfNotExist("files");
- // Table context properties.
- context = new FileContext(account.TableEndpoint.AbsoluteUri, account.Credentials);
- context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
- context.IgnoreResourceNotFoundException = true;
- context.IgnoreMissingProperties = true;
- }
- /// <summary>
- /// Get all entities method.
- /// </summary>
- /// <returns></returns>
- public IEnumerable<FileEntity> GetAllEntities()
- {
- var list = from m in this.context.GetEntities
- select m;
- return list;
- }
- /// <summary>
- /// Get table rows by partitionKey.
- /// </summary>
- /// <param name="partitionKey"></param>
- /// <returns></returns>
- public IEnumerable<FileEntity> GetEntities(string partitionKey)
- {
- var list = from m in this.context.GetEntities
- where m.PartitionKey == partitionKey
- select m;
- return list;
- }
- /// <summary>
- /// Get specify entity.
- /// </summary>
- /// <param name="partitionKey"></param>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public FileEntity GetEntitiesByName(string partitionKey, string fileName)
- {
- var list = from m in this.context.GetEntities
- where m.PartitionKey == partitionKey && m.FileName == fileName
- select m;
- if (list.Count() > 0)
- return list.First<FileEntity>();
- else
- return null;
- }
- /// <summary>
- /// Add an entity.
- /// </summary>
- /// <param name="entity"></param>
- public void AddFile(FileEntity entity)
- {
- this.context.AddObject("files", entity);
- this.context.SaveChanges();
- }
- /// <summary>
- /// Make a judgment to check if file is exists.
- /// </summary>
- /// <param name="filename"></param>
- /// <param name="partitionKey"></param>
- /// <returns></returns>
- public bool FileExists(string filename, string partitionKey)
- {
- IEnumerable<FileEntity> list = from m in this.context.GetEntities
- where m.FileName == filename && m.PartitionKey == partitionKey
- select m;
- if (list.Count()>0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
