2013-03-12 2 views
1

Я начал работу по использованию таблиц хранения Microsoft Windows Azure. Кажется, что все работает нормально - я могу создавать таблицы и вставлять строки с одной проблемой - я не могу вставить строку с чем-то другим, кроме заранее определенных полей ключевых строк, а именно «PartitionKey», «RowKey», и «Временная метка».Таблица хранения Azure - Невозможно добавить определяемое пользователем свойство/поле

В приведенном ниже примере кода, который является просто самым простым «Hello World» в приложении MVC (единственное, что я добавил, это контроллер), вывод показывает, что ожидаемые значения находятся в разделе и строке раздела но что «тестовое поле», которое я пытался вставить, по-прежнему пуст.

Когда вы переходите к коду в отладчике, я вижу, что имя и значение тестового поля, которое я пытаюсь установить, находятся на месте, когда происходит первая таблица. Выполнение() происходит. Но по какой-то причине он фактически не попадает в таблицу.

Любая помощь очень ценится.

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

ответ

2

Вы попробовали преобразовать TestField в свойство с get; set ;?

Попробуйте этот код для 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; 
     } 
    } 
+1

Бинго - спасибо. Бит неловко .... –

+0

Действительно ли это? Нигде не сказано, что это действительно необходимо. –

Смежные вопросы