public async Task UpdateUpsertEntitiesAsync()
        {
            string storageUri        = StorageUri;
            string accountName       = StorageAccountName;
            string storageAccountKey = PrimaryStorageAccountKey;
            string tableName         = "OfficeSupplies5p2" + _random.Next();
            string partitionKey      = "Stationery";
            string rowKey            = "A1";

            var serviceClient = new TableServiceClient(
                new Uri(storageUri),
                new TableSharedKeyCredential(accountName, storageAccountKey));

            await serviceClient.CreateTableAsync(tableName);

            var tableClient = serviceClient.GetTableClient(tableName);

            #region Snippet:TablesSample5UpsertEntityAsync
            var entity = new TableEntity(partitionKey, rowKey)
            {
                { "Product", "Markers" },
                { "Price", 5.00 },
                { "Brand", "myCompany" }
            };

            // Entity doesn't exist in table, so invoking UpsertEntity will simply insert the entity.
            await tableClient.UpsertEntityAsync(entity);

            #endregion

            #region Snippet:TablesSample5UpsertWithReplaceAsync
            // Delete an entity property.
            entity.Remove("Brand");

            // Entity does exist in the table, so invoking UpsertEntity will update using the given UpdateMode, which defaults to Merge if not given.
            // Since UpdateMode.Replace was passed, the existing entity will be replaced and delete the "Brand" property.
            await tableClient.UpsertEntityAsync(entity, TableUpdateMode.Replace);

            #endregion

            #region Snippet:TablesSample5UpdateEntityAsync
            // Get the entity to update.
            TableEntity qEntity = await tableClient.GetEntityAsync <TableEntity>(partitionKey, rowKey);

            qEntity["Price"] = 7.00;

            // Since no UpdateMode was passed, the request will default to Merge.
            await tableClient.UpdateEntityAsync(qEntity, qEntity.ETag);

            TableEntity updatedEntity = await tableClient.GetEntityAsync <TableEntity>(partitionKey, rowKey);

            Console.WriteLine($"'Price' before updating: ${entity.GetDouble("Price")}");
            Console.WriteLine($"'Price' after updating: ${updatedEntity.GetDouble("Price")}");
            #endregion

            await serviceClient.DeleteTableAsync(tableName);
        }
예제 #2
0
        public void UpdateUpsertEntities()
        {
            string storageUri        = StorageUri;
            string accountName       = StorageAccountName;
            string storageAccountKey = PrimaryStorageAccountKey;
            string tableName         = "OfficeSupplies5p1";
            string partitionKey      = "somePartition";
            string rowKey            = "A1";

            var serviceClient = new TableServiceClient(
                new Uri(storageUri),
                new TableSharedKeyCredential(accountName, storageAccountKey));

            serviceClient.CreateTable(tableName);

            #region Snippet:TablesSample5UpsertEntity
            // Get the <see cref="TableClient" /> of the table.
            var client = serviceClient.GetTableClient(tableName);

            // Make an entity.
            var entity = new TableEntity(partitionKey, rowKey)
            {
                { "Product", "Markers" },
                { "Price", 5.00 },
                { "Brand", "myCompany" }
            };

            // Entity doesn't exist in table, so invoking UpsertEntity will simply insert the entity.
            client.UpsertEntity(entity);

            // Delete an entity property.
            entity.Remove("Brand");

            // Entity does exist in the table, so invoking UpsertEntity will update using the given UpdateMode (which defaults to Merge if not given).
            // Since UpdateMode.Replace was passed, the existing entity will be replaced and delete the "Brand" property.
            client.UpsertEntity(entity, TableUpdateMode.Replace);
            #endregion

            #region Snippet:TablesSample5UpdateEntity
            // Query for entities to update.
            Pageable <TableEntity> queryResultsBefore = client.Query <TableEntity>();

            foreach (TableEntity qEntity in queryResultsBefore)
            {
                // Changing property of entity.
                qEntity["Price"] = 7.00;

                // Updating to changed entity using its generated eTag.
                // Since no UpdateMode was passed, the request will default to Merge.
                client.UpdateEntity(qEntity, qEntity.ETag);
            }
            #endregion

            Pageable <TableEntity> queryResultsAfter = client.Query <TableEntity>();
            foreach (TableEntity qEntity in queryResultsAfter)
            {
                Console.WriteLine($"'Price' before updating: ${entity.GetDouble("Price")}");
                Console.WriteLine($"'Price' after updating: ${qEntity.GetDouble("Price")}");
            }

            serviceClient.DeleteTable(tableName);
        }