Creating Azure Table using CloudTableClient.CreateTableIfNotExist

I discussed Three simple steps to create Azure table in my last post. Just after submitting the post, I got a comment from Neil Mackenzie that I can avoid creation of context class [Step 2] in last post. He helped me with the link for the best practice on MSDN

This post is to accommodate suggestion given by him and recommendation given on MSDN.

In this post we will see how to create Azure table using CloudTableClient.CreatTableIfNotExist method.

Add References

If you are creating table from a console application then you need to add below references to your project,

System.Data.Service.Client

clip_image001

And Microsoft.WindowsAzure.Serviceruntime.dll

image

Create Entity class

To represent entity class, we need to create a class inheriting from class TableServiceEntity. SchoolEntity class will be as below,

clip_image001[6]

If you notice in above code snippet there is no

  1. Row Key
  2. Partition Key
  3. Calling base class constructor [ As I discussed here ]

Now when you make object of SchoolEntity class, in intellisense you will get RowKey,PartitionKey and TimeStamp to set as below,

clip_image001[8]

 

Creating Account object

image

Pass connection string as of your storage account.

Creating Table Client

image

Creating Table

image

Creating Table Context

image

Creating Object to Insert

image

Adding Object to table

image

Putting all the code together,


using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
CloudStorageAccount account = CloudStorageAccount.Parse("connectionString");
CloudTableClient tableClient = new CloudTableClient(
account.TableEndpoint.ToString(),
account.Credentials);
string tableName = "School";
tableClient.CreateTableIfNotExist(tableName);

TableServiceContext  context = tableClient.GetDataServiceContext();

SchoolEntity entity = new SchoolEntity
{
Age = 9,
Name = "DJ",
PartitionKey = "S",
RollNumber  = null ,
RowKey = Guid.NewGuid().ToString()
};
context.AddObject(tableName, entity);
context.SaveChanges();

Console.WriteLine("Saved");
Console.ReadKey(true);


}
}

public class SchoolEntity : TableServiceEntity
{
public string Name { get; set; }
public int ? RollNumber { get; set; }
public int Age { get; set; }
}

}

In this way you can create a table. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you

4 responses to “Creating Azure Table using CloudTableClient.CreateTableIfNotExist”

  1. […] Creating Azure Table using CloudTableClient.CreateTableIfNotExist […]

  2. […] Dhananjay Kumar (@debug_mode) explained Inserting Null Value for Integer columns in Windows Azure Table in a 10/12/2011 post: I recommend to read below post before you start reading this post: Creating Azure Table using CloudTableClient.CreateTableIfNotExist. […]

  3. […] Creating Azure Table using CloudTableClient.CreateTableIfNotExist […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com