2

I have a class with an IDictionary. The size of this object isn't fix to any particular size. The idea with this is to have a Dynamic Schema of my object. I want to store this object in the TableStorage. How can I do this? And how can I retrieve this information from store after this inside my object with the IDictionary again??

Thanks!!

4

3 回答 3

1

DynamicTableEntityIDictionary<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;
}
于 2015-04-25T00:05:38.957 回答
0

Dictionaries by default are not serializable. An object must be serializable in order to be saved to TableStorage. I suggest you use List or Array type of objects or write your own serializer for Dictionary if List or Arrays are not good enough for you.

于 2010-11-28T20:14:33.260 回答
0

您可以使用 TableStorageContext 中的 Read 和 Write 事件来执行此操作。您需要将 IDictionary 内容存储在其他字段中或作为 BLOB 文件。在读取事件中,您从给定字段创建 IDictionary 或直接从 BLOB 文件中检索。在写入事件中,您将 IDictionary 存储到字段或作为 BLOB 文件。

重要提示:写入事件发生在实体转换步骤之后,如果您选择字段方式,您可能需要将更改直接写入实体的 XML 序列化。

这听起来很难,但在许多情况下都非常有用

于 2013-08-05T15:59:21.413 回答