通过覆盖从 TableEntity 派生的类的 ReadEntity 和 WriteEntity 方法,可以存储其他属性。
这是我天真的实现
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;
}
}
}
}
我注意到 Azure 表存储总共只能存储 255 个键/值对,或者考虑到 PartitionKey 等后,只能存储 252 个自定义键/值对,因此这也必须在某个地方进行处理。