2
votes

I have been starting work on using Microsoft Windows Azure storage tables. Everything seems to work fine -- I can create tables and insert rows, with one problem -- I can't seem to insert a row with anything other than the pre-defined row key fields, namely "PartitionKey", "RowKey", and "Timestamp".

In the sample code below, which is just the simplest "Hello World" in an MVC application (the only thing I've added is the controller), the output shows that the expected values are in the partition key and row key, but that the "test field" I have tried to insert is still empty.

When stepping in to the code in the debugger, I can see that the name and value of the test field I am trying to set is in place when the first table.Execute() happens. But for some reason, it doesn't actually make it into the table.

Any help much appreciated.

using System;
using System.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Web.Mvc;


namespace HelloWorldApp.Controllers
{
    public class TestEntity : TableEntity
    {
        public string TestField;

        public TestEntity() { }

        public TestEntity(string partitionKey, string rowKey, string testField)
        {
            PartitionKey = partitionKey;
            RowKey = rowKey;

            TestField = testField;
        }
    }

    public class HomeController : Controller
    {
        public string Index()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

            if (storageAccount == null)
                return "Storage Account is Null";

            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            if (tableClient == null)
                return "Table Client is Null";

            CloudTable table = tableClient.GetTableReference("TestTable");
            if (table == null)
                return "Table is Null";

            table.CreateIfNotExists();

            var entity = new TestEntity("MyTestPartitionKey", "MyTestRowKey", "MyTestMessage");

            var doInsert = TableOperation.Insert(entity);

            if (doInsert == null)
                return "Insert Operation is Null";

            // In debugger, I have confirmed that doInsert does include the field TestField="MyTestMessage", yet it doesn't seem to find its way into the table.
            table.Execute(doInsert);

            var doRetrieve = TableOperation.Retrieve<TestEntity>("MyTestPartitionKey", "MyTestRowKey");

            TableResult retrievedResult = table.Execute(doRetrieve);

            if (retrievedResult == null)
                return "Retrieved no rows";

            string retPartitionKey = ((TestEntity) retrievedResult.Result).PartitionKey;
            string retRowKey = ((TestEntity) retrievedResult.Result).RowKey;
            string retTestMessage = ((TestEntity) retrievedResult.Result).TestField;

            return String.Format("Partition: {0}, Row: {1}, TestMessage {2}, remember to delete table.", retPartitionKey, retRowKey, retTestMessage);
        }
    }
}
1

1 Answers

3
votes

Did you try converting TestField to a property with get;set;?

Try this code for your TestEntity:

public class TestEntity : TableEntity
    {
        public string TestField { get; set; }

        public TestEntity() { }

        public TestEntity(string partitionKey, string rowKey, string testField)
        {
            PartitionKey = partitionKey;
            RowKey = rowKey;

            TestField = testField;
        }
    }