2

我已经开始使用 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);
        }
    }
}
4

1 回答 1

3

您是否尝试使用 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;
        }
    }
于 2013-03-13T00:35:46.440 回答