Azure存储表 - 无法添加用户定义的属性/字段

时间:2013-03-12 22:14:04

标签: azure azure-table-storage

我一直在开始使用Microsoft Windows Azure存储表。一切似乎工作正常 - 我可以创建表并插入行,有一个问题 - 我似乎无法插入除预定义的行键字段之外的任何行,即“PartitionKey”,“RowKey”,和“时间戳”。

在下面的示例代码中,它只是MVC应用程序中最简单的“Hello World”(我唯一添加的是控制器),输出显示预期值位于分区键和行键中,但我试图插入的“测试字段”仍然是空的。

当进入调试器中的代码时,我可以看到当第一个table.Execute()发生时,我试图设置的测试字段的名称和值就位。但由于某种原因,它实际上并没有进入表格。

任何帮助都非常感激。

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 个答案:

答案 0 :(得分:2)

您是否尝试使用get; set;?

将TestField转换为属性

为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;
        }
    }