DynamicTableEntity PartitionKey and RowKey

3.2k views Asked by At

I create DynamicTableEntity as follows:

string env = "envTest";
stting ver = "1.0";
siring id = "12356";
string mode = "verify";
DynamicTableEntity entryEntity = DynamicTableEntity(env,ver);
entryEntity.Properties.Add("Id", id);
entryEntity.Properties.Add("Mode", mode);

As a result created a table with columns:"Id", "Mode", "PartitionKey", "RowKey" I want to change the names of "PartitionKey", "RowKey", i.e. I want the env to be a partitionKey but but The column name to be "Env". How can I do it?

2

There are 2 answers

0
Gaurav Mantri On BEST ANSWER

Simple answer is that you can't. PartitionKey and RowKey are system defined attributes (along with Timestamp) and you can't change their name. What you could do is define two more custom attributes for Env and Ver if you need to access the properties by these names in your application.

DynamicTableEntity entryEntity = DynamicTableEntity(env,ver);
entryEntity.Properties.Add("Id", id);
entryEntity.Properties.Add("Mode", mode);
entryEntity.Properties.Add("Env", env);
entryEntity.Properties.Add("Ver", ver);
0
Peter Lea On

Although it's true that you can't rename PartitionKey and RowKey, you can resolve to a DTO straight from the table.

I find this super handy for projecting the data to different audiences (i.e limited view for non-admins etc)

HTH

var query = MyEntityDBO.CreateQuery<DynamicTableEntity>()
            .Where(x => x.PartitionKey.Equals("Blah"))
            .Resolve(MyEntityDTO.GetEntityResolver());
var segment = await query.ExecuteSegmentedAsync(new TableContinuationToken());
if(segment.Results.Count > 0) {
     // Results = IEnumerable<MyEntityDTO>
}

public class MyEntityDTO
{
    public string Id { get; set; }
    public string Mode { get; set;  }
    public string Env { get; set; }
    public string Ver { get; set; }

    public static EntityResolver<MyEntityDTO> GetEntityResolver()
    {
        return (pk, rk, ts, props, etag) =>
        { 
             Env = pk,
             Ver = rk,
             Id = props["Id"].StringValue,
             Mode = props["Mode"].StringValue
        };
    }
}