我有一个带有 IDictionary 的类(class)。该对象的大小不固定为任何特定大小。这个想法是拥有我的对象的动态模式。我想将此对象存储在 TableStorage 中。我怎样才能做到这一点?在我的对象中再次使用 IDictionary 后,如何从存储中检索此信息?
谢谢!!
DynamicTableEntity
需要IDictionary<string,EntityProperty>
作为参数。
此代码在 LinqPad
中执行:
void Main()
{
var account = "";
var key = "";
var tableName = "";
var storageAccount = GetStorageAccount(account, key);
var cloudTableClient = storageAccount.CreateCloudTableClient();
var table = cloudTableClient.GetTableReference(tableName);
var partitionKey = "pk";
var rowKey = "rk";
//create the entity
var entity = new DynamicTableEntity(partitionKey, rowKey, "*",
new Dictionary<string,EntityProperty>{
{"Prop1", new EntityProperty("stringVal")},
{"Prop2", new EntityProperty(DateTimeOffset.UtcNow)},
});
//save the entity
table.Execute(TableOperation.InsertOrReplace(entity));
//retrieve the entity
table.Execute(TableOperation.Retrieve(partitionKey,rowKey)).Result.Dump();
}
static CloudStorageAccount GetStorageAccount(string accountName, string key, bool useHttps = true)
{
var storageCredentials = new StorageCredentials(accountName, key);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps: useHttps);
return storageAccount;
}
我是一名优秀的程序员,十分优秀!