从Azure表存储中检索行

时间:2018-11-21 05:27:00

标签: c# azure .net-core azure-storage azure-table-storage

尝试使用.net core处理Azure存储表操作。我已经成功添加了一个新的存储表,并使用唯一的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 :(得分:1)

将无参数构造函数添加到$(document).ready(function(){ var arr = []; // List of users //function to minimize the chat $(document).on('click','.msg_head',function(){ var chatbox=$(this).parents().attr("rel"); $('[rel="'+chatbox+'"].msg_wrap').slideToggle('slow'); displayChatBox(); return false; }); //function to close the chat $(document).on('click', '.close', function() { var chatbox = $(this).parents().parents().attr("rel") ; $('[rel="'+chatbox+'"]').hide(); arr.splice($.inArray(chatbox, arr), 1); displayChatBox(); return false; }); // function for send button// function sendMessage(e) { /*$("#send_messagessage").addClass("disabled"); if(typeof e.type !== 'string' || (e.type == 'keyup' && e.keyCode != 13)) { //return $('#status').html('no call'); return false; } $('#status').html('made call');*/ var msg = document.getElementById("text_msg").value;; //retrieve msg from text if(msg.trim().length != 0){ var chatbox = $(this).parents().parents().parents().attr("rel") ; $('<div class="msg-right">'+msg+'</div>').insertBefore('[rel="'+chatbox+'"] .msg_push'); $('.msg_body').scrollTop($('.msg_body')[0].scrollHeight); var product = output(msg); $('<div class="msg-left">'+product+'</div>').insertBefore('[rel="'+chatbox+'"] .msg_push'); $('.msg_body').scrollTop($('.msg_body')[0].scrollHeight); } }; $(document).on('click', '#sidebar-user-box', function() { var userID = $(this).attr("class"); var username = $(this).children().text() ; if ($.inArray(userID, arr) != -1) { arr.splice($.inArray(userID, arr), 1); } arr.unshift(userID); chatPopup = '<div class="msg_box" rel="'+ userID+'">'+ '<div class="msg_head">'+username + '<div class="close">X</div> </div>'+ '<div class="msg_wrap"> <div class="msg_body"> <div class="msg_push"></div> </div>'+ '<div class="msg_footer"><input class="msg_input" placeholder="Enter Message..." type="text"><button id="send_message" type="submit" >Send</button>'+ '</div></div></div>' ; $("body").append( chatPopup ); displayChatBox(); }); $(document).on('keypress', 'input' , function(e) { if (e.keyCode == 13 ) { var msg = $(this).val(); $(this).val(""); if(msg.trim().length != 0){ var chatbox = $(this).parents().parents().parents().attr("rel") ; $('<div class="msg-right">'+msg+'</div>').insertBefore('[rel="'+chatbox+'"] .msg_push'); $('.msg_body').scrollTop($('.msg_body')[0].scrollHeight); var product = output(msg); $('<div class="msg-left">'+product+'</div>').insertBefore('[rel="'+chatbox+'"] .msg_push'); $('.msg_body').scrollTop($('.msg_body')[0].scrollHeight); } } }); function output(input){ try{ var product = input + "=" + eval(input); } catch(e){ var text = (input.toLowerCase()).replace(/[^\w\s\d]/gi, ""); //remove all chars except words, space and text = text.replace(/ a /g, " ").replace(/i feel /g, "").replace(/whats/g, "what is").replace(/please /g, "").replace(/ please/g, ""); if(compare(trigger, reply, text)){ var product = compare(trigger, reply, text); } else { var product = alternative[Math.floor(Math.random()*alternative.length)]; } } return product; } function compare(arr, array, string){ var item; for(var x=0; x<arr.length; x++){ for(var y=0; y<array.length; y++){ if(arr[x][y] == string){ items = array[x]; item = items[Math.floor(Math.random()*items.length)]; } } } return item; } function displayChatBox(){ i = 0 ; // start position j = 260; //next position $.each( arr, function( index, value ) { if(index < 4){ $('[rel="'+value+'"]').css("right",i); $('[rel="'+value+'"]').show(); i = i+j; } else{ $('[rel="'+value+'"]').hide(); } }); } }); 类中可以解决此问题:

BlogEntity

希望有帮助!