Search code examples
.net-coreazure-table-storageazure-sdk-.net

Azure Table Storage: Use existing properties as PartitionKey and RowKey


I have a model where existing fields already fits quite nicely for RowKey and PartitionKey but I don't know how to assign them and not have duplicate data. Consider this example:

public class ClassA : ITableEntity
{
    // My existing fields
    public string Id { get; set; }
    public string Receiver { get; set; }
    public DateTimeOffset? Received { get; set; }

    // ITableEntity fields
    public string PartitionKey { get => Receiver; set => Receiver = value; }
    public string RowKey { get => Id; set => Id = value; }
    public DateTimeOffset? Timestamp { get => Received; set => Received = value; }
    public ETag ETag { get; set; }
}

This works but we get duplicate data as expected. Then I tried to add [IgnoreDataMember] to Id, Receiver and Received but then I cannot query using TableClient.QueryAsync(x => x.Id == "...").

Is there a way I can map the ITableEntity to my exisiting fields so I:

  1. Avoid having duplicate data
  2. Can query using my exisiting fields

I'm using Azure.Data.Tables Version 12.4.0


Solution

  • With Table Storage, it is not possible to designate any attributes as PartitionKey and RowKey attribute. The attributes must be named PartitionKey and RowKey.

    You can query on any existing fields but the query will not be treated the same as querying on PartitionKey/RowKey fields and will result in full table scan.