Inserting Null Value for Integer columns in Windows Azure Table

I recommend to read  below post before you start reading this post

Creating Azure Table using CloudTableClient.CreateTableIfNotExist

There may be requirement when you need to insert null values for values type columns in Azure table. In our recent project, we had a requirement to insert null values for integer columns. Usually if you don’t provide any value to Integer variable, it would get initialized as 0, since they are value type’s variables. This post will focus on providing null values for integer columns

Very first you need to define and entity as below,

image

 

You may have noticed that I am making integer properties as Nullable types

image

Now create Azure table as below,

image

Then you can insert null value for integer columns as below,

image

For your reference full source code to insert null values is as below,


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 = null,
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 insert null values. 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

Three simple steps to create Azure table

Please read post at below link for recommended way to create table,

https://debugmode.net/2011/10/12/creating-azure-table-using-cloudtableclient-createtableifnotexist/

In this post I will show you three simple steps to create Azure Table.

Let us say you want to create Azure table called Employee with following columns

  1. EmployeeID
  2. EmployeeName
  3. EmployeeSalary

Step 1: Create Entity class

First step is to create entity class representing employee.

Each row of azure table contains at least below columns

  1. Row key
  2. Partition Key
  3. Time Stamp

And custom columns along with above columns. So Employee table will be having all together below columns

  1. Row Key
  2. Partition Key
  3. Time Stamp
  4. EmployeeID
  5. EmployeeName
  6. EmployeeSalary

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


using System;
using Microsoft.WindowsAzure.StorageClient;

namespace Test
{
public class EmployeeEntity : TableServiceEntity
{
public EmployeeEntity(string partitionKey, string rowKey)
: base (partitionKey,rowKey )
{

}
public EmployeeEntity()
: base()
{
PartitionKey = "test";
RowKey = String.Empty;
}

public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int EmployeeSalaray { get; set; }
}
}

 

There is couple of points worth discussing about above class.

  1. It inherits from TableServiceEntity class
  2. It is having two constructors
  3. Passing rowkey and partitionkey as string value to base constructor
  4. Custom columns are as the public property of the class.

Step 2: Create Data Context class

Once Entity class is in place, we need to create DataContext class. This class will be used to create the table.


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

namespace Test
{
public class EmployeeContext : TableServiceContext
{
internal EmployeeContext(string baseAddress, StorageCredentials credentials)
:
base(baseAddress, credentials)
{

}

internal const string EmployeeTableName = "Employee";
public IQueryable<EmployeeEntity> Employee
{
get
{
return this.CreateQuery<EmployeeEntity>(EmployeeTableName);
}
}
}
}

There are couples of points worth discussing about above code

  • It inherits from TableServiceContext class
  • In constructor we are passing credentials
  • In constructor we are passing base address to create the table.

clip_image002

  • Table name is given inline in the code as below; Employee is name of the table we want to create.

clip_image003

  • We need to add IQueyable property to create query.

clip_image004

Step 3: Creating Table

To create table we need a very simple code.

  1. Create the account. In below code DataConnectionString is name of the connection string. This could be pointing to local storage or Azure Tables.
  2. We are creating table from Data Model. Passing EmployeeContext class as parameter.

using System;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using System.Linq;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace Test
{
public class CrudClass
{
public class CrudClass
{
public void CreateTable()
{
CloudStorageAccount account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));
CloudTableClient.CreateTablesFromModel(typeof(EmployeeContext
), account.TableEndpoint.AbsoluteUri, account.Credentials);

}

}
}

&nbsp;

When you call CreateTable() method of CrudClass , it will create Employee table for you in Azure 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