/// <summary> /// The Table Service supports two main types of insert operations. /// 1. Insert - insert a new entity. If an entity already exists with the same PK + RK an exception will be thrown. /// 2. Replace - replace an existing entity. Replace an existing entity with a new entity. /// 3. Insert or Replace - insert the entity if the entity does not exist, or if the entity exists, replace the existing one. /// 4. Insert or Merge - insert the entity if the entity does not exist or, if the entity exists, merges the provided entity properties with the already existing ones. /// </summary> /// <param name="table">The sample table name</param> /// <param name="entity">The entity to insert or merge</param> /// <returns></returns> private static async Task<CustomerEntity> InsertOrMergeEntityAsync(CloudTable table, CustomerEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } // Create the InsertOrReplace TableOperation TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity); // Execute the operation. TableResult result = await table.ExecuteAsync(insertOrMergeOperation); CustomerEntity insertedCustomer = result.Result as CustomerEntity; return insertedCustomer; }
/// <summary> /// Delete an entity /// </summary> /// <param name="table">Sample table name</param> /// <param name="deleteEntity">Entity to delete</param> private static async Task DeleteEntityAsync(CloudTable table, CustomerEntity deleteEntity) { if (deleteEntity == null) { throw new ArgumentNullException("deleteEntity"); } TableOperation deleteOperation = TableOperation.Delete(deleteEntity); await table.ExecuteAsync(deleteOperation); }
/// <summary> /// Demonstrate basic Table CRUD operations. /// </summary> /// <param name="table">The sample table</param> private static async Task BasicTableOperationsAsync(CloudTable table) { // Create an instance of a customer entity. See the Model\CustomerEntity.cs for a description of the entity. CustomerEntity customer = new CustomerEntity("Harp", "Walter") { Email = "*****@*****.**", PhoneNumber = "425-555-0101" }; // Demonstrate how to Update the entity by changing the phone number Console.WriteLine("2. Update an existing Entity using the InsertOrMerge Upsert Operation."); customer.PhoneNumber = "425-555-0105"; customer = await InsertOrMergeEntityAsync(table, customer); // Demonstrate how to Read the updated entity using a point query Console.WriteLine("3. Reading the updated Entity."); customer = await RetrieveEntityUsingPointQueryAsync(table, "Harp", "Walter"); // Demonstrate how to Delete an entity Console.WriteLine("4. Delete the entity. "); await DeleteEntityAsync(table, customer); }