![](/img/trans.png)
[英]Azure table storage: How to get a single entity by a partition key only?
[英]Azure Table Storage issue getting single entity
我正在創建一個非常簡單的表存儲,它存儲一個 int 值。
但是,我無法獲取代碼以從存儲中獲取實體工作。
model class:
public class SomeClass: ITableEntity
{
public SomeClass(string someKey, int someNumber)
{
PartitionKey = someKey;
RowKey = someKey;
ETag = new ETag("*");
SomeNumber= someNumber;
}
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset? Timestamp { get; set; } = DateTimeOffset.Now;
public ETag ETag { get; set; }
public int SomeNumber { get; }
}
客戶端class如下:
public class AzureTableClient : IAzureTableClient
{
private readonly IConfiguration _configuration;
private readonly TableServiceClient _tableServiceClient;
public AzureTableClient(IConfiguration configuration, TableServiceClient tableServiceClient)
{
_configuration = configuration;
_tableServiceClient = tableServiceClient;
}
public void CreateEntity(string someKey, int count)
{
var tableClient = CreateTableServiceClient();
var response = tableClient.UpsertEntity(new SomeClass(someKey, count));
}
public int GetSomeNumber(string someKey)
{
var tableClient = CreateTableServiceClient();
return tableClient.GetEntity<TodaysZipCount>(rowKey: someKey, partitionKey: someKey).Value.SomeNumber;
}
private TableClient CreateTableServiceClient()
{
var tableName = _configuration.GetValue<string>("TableName");
var tableClient =_tableServiceClient.GetTableClient(tableName);
tableClient.CreateIfNotExists();
return tableClient;
}
}
以下代碼給了我以下錯誤消息:
return tableClient.GetEntity<TodaysZipCount>(rowKey: dateKey, partitionKey: dateKey).Value.TodaysCount;
錯誤:
Error CS0310 'SomeClass' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'TableClient.GetEntity<T>(string, string, IEnumerable<string>, CancellationToken)'
我已經按照文檔中的內容完成了所有工作......這里出了什么問題?
正如錯誤消息所說,您需要為您的實體創建一個public parameterless constructor
。
請加
public SomeClass()
{
}
到您的SomeClass
定義,錯誤應該 go 消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.