从 Azure table 存储中检索行
Retrieving row from Azure table storage
尝试使用 .net 核心处理 Azure 存储 table 操作。我已经成功添加了一个新存储 table 并插入了具有唯一 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.
博客实体 class :
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
class 添加一个无参数构造函数应该可以解决问题:
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; }
}
希望对您有所帮助!
尝试使用 .net 核心处理 Azure 存储 table 操作。我已经成功添加了一个新存储 table 并插入了具有唯一 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.
博客实体 class :
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
class 添加一个无参数构造函数应该可以解决问题:
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; }
}
希望对您有所帮助!