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

标签 azure azure-table-storage

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

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

当进入调试器中的代码时,我可以看到我尝试设置的测试字段的名称和值在第一个 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);
        }
    }
}

最佳答案

您是否尝试使用 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;
        }
    }

关于Azure 存储表 - 无法添加用户定义的属性/字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373275/

相关文章:

c# - 是否可以将 Azure 移动服务与 WP7 一起使用

azure - ARM 模板循环/循环依赖解析

使用 SAML 和 azure Active Directory 的 Azure 自定义应用程序 SSO

python - 使用 Azure CLI、Rest API 或 Python 在 Azure ADLS gen2 中复制文件

c# - 对许多表使用与 Controller 相同的类

angular - 将 Angular 4 发布到 Azure

azure - Azure BLOB 的实体组事务?

java - Azure容器实例(Windows)是否支持访问表存储

Azure: worker 角色循环通过 "recycling"

Azure表存储投影查询和未实现异常