1

我有多个实体要存储在同一个物理 Azure 表中。我正在尝试从文件中插入/合并表条目。我正在尝试找到一种方法来做到这一点,而无需真正序列化每个属性或为此创建自定义实体。

在尝试以下代码时,我想也许我可以使用通用的 DynamicTableEntity。但是,我不确定它是否有助于插入操作(大多数文档用于替换/合并操作)。

我得到的错误是 HResult=-2146233088 Message=Unexpected response code for operation : 0 Source=Microsoft.WindowsAzure.Storage

任何帮助表示赞赏。

这是我的代码的摘录

_tableClient = storageAccount.CreateCloudTableClient();
            _table = _tableClient.GetTableReference("CloudlyPilot");

            _table.CreateIfNotExists();


            TableBatchOperation batch = new TableBatchOperation();

            ....

             foreach (var pkGroup in result.Elements("PartitionGroup"))
            {

                foreach (var entity in pkGroup.Elements())
                {
                    DynamicTableEntity tableEntity = new DynamicTableEntity();
                    string partitionKey = entity.Elements("PartitionKey").FirstOrDefault().Value;
                    string rowKey = entity.Elements("RowKey").FirstOrDefault().Value;
                    Dictionary<string, EntityProperty> props = new Dictionary<string, EntityProperty>();

                    //if (pkGroup.Attribute("name").Value == "CloudServices Page")
                    //{
                    //    tableEntity = new CloudServicesGroupEntity (partitionKey, rowKey);
                    //}
                    //else
                    //{
                    //    tableEntity = new CloudServiceDetailsEntity(partitionKey,rowKey);
                    //}

                    foreach (var element in entity.Elements())
                    {
                        tableEntity.Properties[element.Name.ToString()] = new EntityProperty(element.Value.ToString());
                    }

                    tableEntity.ETag = Guid.NewGuid().ToString();
                    tableEntity.Timestamp = new DateTimeOffset(DateTime.Now.ToUniversalTime());

                    //tableEntity.WriteEntity(/*WHERE TO GET AN OPERATION CONTEXT FROM?*/)

                    batch.InsertOrMerge(tableEntity);
                }
                _table.ExecuteBatch(batch);

                batch.Clear();

            }
4

1 回答 1

3

您是否尝试过使用DictionaryTableEntity?此类允许您像字典一样动态填充实体(类似于 DynamicTableEntity)。我尝试了类似你的代码的东西,它可以工作:

        var batch = new TableBatchOperation();

        var entity1 = new DictionaryTableEntity();
        entity1.PartitionKey = "abc";
        entity1.RowKey = Guid.NewGuid().ToString();
        entity1.Add("name", "Steve");
        batch.InsertOrMerge(entity1);

        var entity2 = new DictionaryTableEntity();
        entity2.PartitionKey = "abc";
        entity2.RowKey = Guid.NewGuid().ToString();
        entity2.Add("name", "Scott");
        batch.InsertOrMerge(entity2);

        table.ExecuteBatch(batch);

        var entities = table.ExecuteQuery<DictionaryTableEntity>(new TableQuery<DictionaryTableEntity>());

最后一件事,我看到您正在自己设置时间戳和 ETag。删除这两行,然后重试。

于 2012-12-31T08:27:53.460 回答