gpt4 book ai didi

c# - 从 Azure 表存储中检索行

转载 作者:行者123 更新时间:2023-12-03 05:46:29 25 4
gpt4 key购买 nike

尝试使用 .net core 处理 Azure 存储表操作。我已经成功添加了一个新的存储表,并插入了具有唯一 rowKey 的新行(我通过重新插入测试了插入,这给了我冲突,这意味着它已经有一个键)。当我尝试使用 rowKey =1 检索已插入的相同 key 时,出现错误。

        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.

博客实体类:

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

最佳答案

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

希望对你有帮助!

关于c# - 从 Azure 表存储中检索行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53405731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com