简体   繁体   中英

Azure Table Storage issue getting single entity

I'm creating a very simple Table Storage which stores an int value.

However I'm unable to get the code to get an entity from the storage to work.

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

The client class below:

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

The following code gives me the following error message:

return tableClient.GetEntity<TodaysZipCount>(rowKey: dateKey, partitionKey: dateKey).Value.TodaysCount;

Error:

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)'    

I've done everything exactly as found in the documentation ... What's going wrong here?

As the error message says, you need to create a public parameterless constructor for your entity.

Please add

public SomeClass()
{
}

to your SomeClass definition and the error should go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM