Friday 9 December 2011

Updating Table Entity In Azure Table Storage With Out Querying Table Entity:-


public void Update()
{
   //specify storage credentials.
   StorageCredentialsAccountAndKey credentials = new      StorageCredentialsAccountAndKey("storageaccountname","key");

  //create a reference to your storage account, passing in your credentials.

  CloudStorageAccountstorageAccount = new CloudStorageAccount(credentials ,   true);


  //create object  to CloudTableClient to access  the Table service.

  CloudTableClient tableClient = new CloudTableClient   (storageAccount.TableEndpoint.ToString(),
storageAccount.Credentials);


   //creates the  table if not exist
tableClient.CreateTableIfNotExist("TableName");

   //get data context
   TableServiceContext context = tableClient.GetDataServiceContext();

  //table object which inherits from TableServiceEntity
  TableEntity obj = new TableEntity();


  //partitionkey of the existing table entity
  obj.PartitionKey= partitionKey;


  //rowkey of the existing table entity
  obj.RowKey = rowkey;

  //user id propery
  obj.UserId= userId;


  //this method forces the context to track on the specified entity
  context.AttachTo("TableName", obj, "*");


  //this method updates the object
  context.UpdateObject(obj);


  //this method replaces the old object with new one.
  context.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
}
  
 //table entity class
 public class TableEntity :TableServiceEntity
 {
    //constructor
    public TableEntity()
   {

   }
    //user id propery
    public string UserId{get;set;}

 }



Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

No comments:

Post a Comment