// Insert a new contact. private static void InsertContactInternal(TableServiceContext context, string firstName, string lastName, string email, string cellPhone, string homePhone, string streetAddress, string city, string state, string zipCode) { // Create the new entity. ContactEntity entity = new ContactEntity(); // Partition key is first letter of contact's first name. entity.PartitionKey = firstName.Substring(0, 1).ToUpper(); // Row key is value of first name, with GUID appended to avoid conflicts in case where two first names are the same. entity.RowKey = firstName + "_" + Guid.NewGuid().ToString(); // Populate the other properties. entity.FirstName = firstName; entity.LastName = lastName; entity.Email = email; entity.CellPhone = cellPhone; entity.HomePhone = homePhone; entity.StreetAddress = streetAddress; entity.City = city; entity.State = state; entity.ZipCode = zipCode; // Add the entity. context.AddObject(tableName, entity); }
// Delete a contact. public static void DeleteContact(string rowKey) { // Get data context. TableServiceContext context = tableClient.GetDataServiceContext(); // Retrieve contact. ContactEntity entity = GetContact(rowKey, context); // Delete the entity. context.DeleteObject(entity); // Save changes to the service. context.SaveChanges(); }
// Update contact data. public static void UpdateContact( string rowKey, string firstName, string lastName, string email, string cellPhone, string homePhone, string streetAddress, string city, string state, string zipCode) { // Update the contact if the sort position did not change. if (rowKey.StartsWith(firstName + "_")) { // Get data context. TableServiceContext context = tableClient.GetDataServiceContext(); // Set updated values ContactEntity entity = GetContact(rowKey, context); entity.FirstName = firstName; entity.LastName = lastName; entity.Email = email; entity.CellPhone = cellPhone; entity.HomePhone = homePhone; entity.StreetAddress = streetAddress; entity.City = city; entity.State = state; entity.ZipCode = zipCode; // Update the object. context.UpdateObject(entity); // Write changes to the Table service. context.SaveChanges(); } else { // Delete the contact and insert a new one with new keys DeleteContact(rowKey); InsertContact(firstName, lastName, email, cellPhone, homePhone, streetAddress, city, state, zipCode); } }