How can I store arbitrary key value pairs in Azure table storage?

大城市里の小女人 提交于 2019-12-05 08:07:09

By overriding the ReadEntity and WriteEntity methods on classes derived from TableEntity, additional properties can be stored.

Here's my naive implementation

public class TestEntity : TableEntity {

    public TestEntity(string a, string b) {
        PartitionKey = a;
        RowKey = b;
        DataItems = new Dictionary<string, string>();
    }

    public Dictionary<string, string> DataItems { get; set; }

    public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext) {
        var results = base.WriteEntity(operationContext);
        foreach (var item in DataItems) {
            results.Add("D_" + item.Key, new EntityProperty(item.Value));
        }
        return results;
    }

    public override void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext) {
        base.ReadEntity(properties, operationContext);

        DataItems = new Dictionary<string, string>();

        foreach (var item in properties) {
            if (item.Key.StartsWith("D_")) {
                string realKey = item.Key.Substring(2);
                ItemData[realKey] = item.Value.StringValue;
            }
        }
    }
}

I've noted that Azure table storage can only store a total of 255 key/value pairs – or 252 custom ones once PartitionKey, etc. is taken into account, so this has to be handled somewhere as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!