GlobalDemo.DAL.Azure.StorageRepository.SaveToTableStorageAsync C# (CSharp) Method

SaveToTableStorageAsync() public method

Saves an entity to Azure table storage
public SaveToTableStorageAsync ( string tableName, PhotoModel model ) : Task
tableName string The name of the table to save to
model GlobalDemo.DAL.Models.PhotoModel The PhotoModel object to be saved
return Task
        public async Task<string> SaveToTableStorageAsync(            
            string tableName,
            PhotoModel model)
        {
            //We use the DateAdded field for cross-partition queries
            DateTime now = System.DateTime.Now;

            model.DateAdded = now;

            PhotoEntity entity = new PhotoEntity(model);
            
            //These properties are used in a full table scan to populate
            //all photos for all users.  Needed as some way to get the 
            //items for a single day for all users.
            entity.DayAdded = now.Day;
            entity.MonthAdded = now.Month;
            entity.YearAdded = now.Year;
            
            var client = _account.CreateCloudTableClient();
            var table = client.GetTableReference(tableName);
            var operation = TableOperation.InsertOrReplace(entity);

            var result = await table.ExecuteAsync(operation);

            //TODO:  Do we need to check the HTTP status code here?
            return result.HttpStatusCode.ToString();
        }

Usage Example

示例#1
0
        internal static async Task SaveToTableStorageAsync(PhotoModel p, TextWriter log)
        {
            var storageConnectionString = SettingsHelper.LocalStorageConnectionString;
            var repo = new StorageRepository(storageConnectionString);
            var result = await repo.SaveToTableStorageAsync(DAL.Azure.StorageConfig.TableName, p);

            await log.WriteLineAsync("Save to table HTTP result: " + result);
        }