2
votes

I have an Azure Table Storage with the following Entity:

SampleEntity : TableEntity
{
  public int EmployeeId{get; set;}
}

And I have inserted 100 records to the table. Now I have a change in requirement that the EmployeeID should be string. Also I should not delete the existing 100 records that were inserted. Hence I changed the existing SampleEntity as follows:

SampleEntity : TableEntity
{
  public string EmployeeId{get; set;}
}

And I have inserted 50 Rows into the table with EmployeeId as string.

Now when I do a GetOperation on the table with new SampleEntity(with string EmployeeID), I am getting 150 rows, but the values of EmployeeID for the first 100 rows inserted using the Old SampleEntity was 0.

On the other hand if I switch to old SampleEntity and do a GetOperaiton, I get null values for EmployeeID for the 50 rows inserted using new SampleEntity.

How can I use the new Sample Entity toget all 150 rows with values for EmployeeId in Strings?

1
Why mysql, sql, sql-server tags?Gaurav Mantri
Will you be OK with changing the integer EmployeeId to string EmployeeId (without deleting them of course) or do you want to preserve the data types?Gaurav Mantri
@GauravMantri: I do not want to preserve data types.Rockstart

1 Answers

3
votes

What you could possibly do is change the data type of Integer type EmployeeId to String. For this what you would need to do is fetch all entities as DynamicTableEntity and check the property type of EmployeeId property. If the type is Int32, you would create a new entity with a String type EmployeeId property and set its value to old entity's EmployeeId value and then update the existing entity (you will keep the same PartitionKey and RowKey).

See the sample code below for example:

        //Get all entities and make sure we get them as dynamic table entity.
        var query = new TableQuery();
        var allEntities = table.ExecuteQuery(query);
        foreach (var entity in allEntities)
        {
            var propertyType = entity.Properties["EmployeeId"].PropertyType;
            if (propertyType == EdmType.Int32 && entity.Properties["EmployeeId"].Int32Value.HasValue)
            {
                //This is an entity with Integer type EmployeeId. What we need to do is update this entity with a new entity where data type of EmployeeId is String.
                var employeeId = entity.Properties["EmployeeId"].Int32Value.Value;
                var newEntityWithStringType = new DynamicTableEntity()
                {
                    PartitionKey = entity.PartitionKey,
                    RowKey = entity.RowKey,
                    ETag = "*"
                };
                newEntityWithStringType.Properties.Add("EmployeeId", new EntityProperty(employeeId.ToString()));
                TableOperation updateOperation = TableOperation.Replace(newEntityWithStringType);
                table.Execute(updateOperation);
            }
        }

The code above assumes that you only have one property EmployeeId. If there are more properties, please make sure to include them in newEntityWithStringType Properties.