Получение строки из хранилища таблиц Azure - PullRequest
0 голосов
/ 21 ноября 2018

Попытка обработки операций таблицы хранения Azure с использованием ядра .net.Я успешно добавил новую таблицу хранения и вставил новую строку с уникальным rowKey (я протестировал вставку с повторной вставкой, что дало мне конфликт, что означает, что у него уже есть ключ).Когда я пытаюсь получить тот же ключ с rowKey = 1, который я вставил, появляется ошибка.

        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString);

        //create storage table
        CreateTable(cloudStorageAccount).GetAwaiter().GetResult();
        InsertEntity(cloudStorageAccount).GetAwaiter().GetResult();
        RetrieveEntity(cloudStorageAccount, "blog", "1").GetAwaiter().GetResult();

public static async Task RetrieveEntity(CloudStorageAccount cloudStorageAccount, 
        string partitionKey, string rowKey)
    {
        var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
        var cloudTableReference = cloudTableClient.GetTableReference("MyAzureTableStorage");
        TableOperation retrieve = TableOperation.Retrieve<BlogEntity>(partitionKey, rowKey);
        var result = await cloudTableReference.ExecuteAsync(retrieve);
        if(result.Result == null)
        {
            Console.WriteLine("Not able to find the results");
        }
        else
        {
            Console.WriteLine($"Blog found for author: {((BlogEntity)result.Result).Author}");
        }

    }

Получение ошибки:

Microsoft.WindowsAzure.Storage.StorageException
  HResult=0x80131500
  Message=No parameterless constructor defined for this object.
  Source=Microsoft.WindowsAzure.Storage
  StackTrace:
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.    <ExecuteAsyncInternal>d__4`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at     System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at AzureTable.Program.<RetrieveEntity>d__3.MoveNext() in D:\Manish    \Practice\AzureIt\AzureTable\Program.cs:line 46
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at AzureTable.Program.Main(String[] args) in D:\Manish\Practice\AzureIt\AzureTable\Program.cs:line 21

Inner Exception 1:
MissingMethodException: No parameterless constructor defined for this object.

Класс BlogEntity:

public class BlogEntity : TableEntity
{
    public BlogEntity(int ID, string author, string title, string description)
    {
        this.UniqueID = ID;
        this.Author = author;
        this.Title = title;
        this.Description = description;
        this.PartitionKey = "blog";
        this.RowKey = ID.ToString();
    }

    public int UniqueID { get; set; }
    public string Author { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

1 Ответ

0 голосов
/ 21 ноября 2018

Добавление в класс BlogEntity конструктора без параметров должно решить проблему:

public class BlogEntity : TableEntity
{
    public BlogEntity() { }

    public BlogEntity(int ID, string author, string title, string description)
    {
        this.UniqueID = ID;
        this.Author = author;
        this.Title = title;
        this.Description = description;
        this.PartitionKey = "blog";
        this.RowKey = ID.ToString();
    }

    public int UniqueID { get; set; }
    public string Author { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

Надеюсь, это поможет!

...