gpt4 book ai didi

c# - Azure:如何在表存储中保存 IDictionary

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:11 25 4
gpt4 key购买 nike

我有一个带有 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;
}

关于c# - Azure:如何在表存储中保存 IDictionary<String>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com