DynamicTableEntity PartitionKey and RowKey

I am creating DynamicTableEntity like this:

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, a table is created with columns: "Id", "Mode", "PartitionKey", "RowKey", I want to change the names of "PartitionKey", "RowKey", i.e. I want env to be a partitionKey element, but the column name is "Env". How can i do this?

+3


source to share


2 answers


The simple answer is that you cannot. PartitionKey

and RowKey

are system attributes (along with Timestamp

) and you cannot change their name. What you can do is define two more custom attributes for Env

and Ver

if you need to access 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);

      

+4


source


While it is true that you cannot rename PartitionKey and RowKey, you can resolve DTOs directly from the table.

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



NTN

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

      

+6


source







All Articles